diff --git a/api/accomodation/gate.controllers.go b/api/accomodation/gate.controllers.go index 49a7075b..b4c4c7df 100644 --- a/api/accomodation/gate.controllers.go +++ b/api/accomodation/gate.controllers.go @@ -79,8 +79,9 @@ func MapQrStudentId(c *gin.Context) { // Check if accommodation exists (can be NULL if student hasn't opted) if res.AccommodationID == uuid.Nil { - c.JSON(http.StatusNotFound, gin.H{ - "message": "Student has not opted for accommodation", + c.JSON(http.StatusOK, gin.H{ + "message": "Student has not opted for accommodation", + "has_opted_accommodation": res.HasOptedAccommodation, }) pkg.Log.WarnCtx(c, "[MAP-QR-STUDENT-WARN]: Student has no accommodation opted") return diff --git a/api/accomodation/panel.controllers.go b/api/accomodation/panel.controllers.go index 3e8bfd3c..e3fbfbe6 100644 --- a/api/accomodation/panel.controllers.go +++ b/api/accomodation/panel.controllers.go @@ -210,6 +210,22 @@ func AllotHostel(c *gin.Context) { q := db.New() + accommodation, err := q.GetAccommodationByIdQuery(ctx, tx, accommodationId) + if err == pgx.ErrNoRows { + c.JSON(http.StatusNotFound, gin.H{ + "message": "Accommodation request not found", + }) + pkg.Log.WarnCtx(c, "[ALLOT-HOSTEL-WARN]: Accommodation ID does not exist") + return + } + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "Oops! Something happened. Please try again later.", + }) + pkg.Log.ErrorCtx(c, "[ALLOT-HOSTEL-ERROR]: Failed to fetch accommodation request", err) + return + } + hostel, err := q.GetHostelQuery(ctx, tx, hostelIdUuid) if err == pgx.ErrNoRows { c.JSON(http.StatusNotFound, gin.H{ @@ -226,6 +242,15 @@ func AllotHostel(c *gin.Context) { return } + if accommodation.IsMale != hostel.IsMale { + c.JSON(http.StatusBadRequest, gin.H{ + "message": "Hostel gender does not match student gender", + }) + pkg.Log.WarnCtx(c, + "[ALLOT-HOSTEL-WARN]: Hostel gender does not match student gender") + return + } + if hostel.RoomCount <= 0 { c.JSON(http.StatusOK, gin.H{ "message": "No rooms available in the selected hostel", @@ -256,6 +281,7 @@ func AllotHostel(c *gin.Context) { row, err = q.AllotHostelQuery(ctx, tx, db.AllotHostelQueryParams{ ID: accommodationId, HostelID: hostleIdPgUuid, + DayCount: req.DayCount, }) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ diff --git a/db/gen/accomodation-for-hospitality-panel.sql.go b/db/gen/accomodation-for-hospitality-panel.sql.go index b29f68ff..3f83d99b 100644 --- a/db/gen/accomodation-for-hospitality-panel.sql.go +++ b/db/gen/accomodation-for-hospitality-panel.sql.go @@ -70,6 +70,7 @@ const allotHostelQuery = `-- name: AllotHostelQuery :execrows UPDATE accomodation_details SET hostel_id = $2, + day_count = $3, payment_expires = NOW() + INTERVAL '30 minutes', payment_status = 'PENDING', updated_at = NOW() @@ -83,10 +84,11 @@ SET type AllotHostelQueryParams struct { ID uuid.UUID `json:"id"` HostelID pgtype.UUID `json:"hostel_id"` + DayCount int32 `json:"day_count"` } func (q *Queries) AllotHostelQuery(ctx context.Context, db DBTX, arg AllotHostelQueryParams) (int64, error) { - result, err := db.Exec(ctx, allotHostelQuery, arg.ID, arg.HostelID) + result, err := db.Exec(ctx, allotHostelQuery, arg.ID, arg.HostelID, arg.DayCount) if err != nil { return 0, err } @@ -257,7 +259,7 @@ const getAllHostelDetailsQuery = `-- name: GetAllHostelDetailsQuery :many SELECT hm.id AS hostel_id, hm.hostel_name AS hostel_name, - hm.room_count AS available_rooms, + hm.room_count AS room_count, hm.is_male AS is_male, hm.latitude AS latitude, hm.longtitude AS longtitude, @@ -268,15 +270,15 @@ FROM hostel_metadata hm ` type GetAllHostelDetailsQueryRow struct { - HostelID uuid.UUID `json:"hostel_id"` - HostelName string `json:"hostel_name"` - AvailableRooms int32 `json:"available_rooms"` - IsMale bool `json:"is_male"` - Latitude pgtype.Text `json:"latitude"` - Longtitude pgtype.Text `json:"longtitude"` - MapUrl pgtype.Text `json:"map_url"` - WardenEmail pgtype.Text `json:"warden_email"` - RoomFilled int32 `json:"room_filled"` + HostelID uuid.UUID `json:"hostel_id"` + HostelName string `json:"hostel_name"` + RoomCount int32 `json:"room_count"` + IsMale bool `json:"is_male"` + Latitude pgtype.Text `json:"latitude"` + Longtitude pgtype.Text `json:"longtitude"` + MapUrl pgtype.Text `json:"map_url"` + WardenEmail pgtype.Text `json:"warden_email"` + RoomFilled int32 `json:"room_filled"` } func (q *Queries) GetAllHostelDetailsQuery(ctx context.Context, db DBTX) ([]GetAllHostelDetailsQueryRow, error) { @@ -291,7 +293,7 @@ func (q *Queries) GetAllHostelDetailsQuery(ctx context.Context, db DBTX) ([]GetA if err := rows.Scan( &i.HostelID, &i.HostelName, - &i.AvailableRooms, + &i.RoomCount, &i.IsMale, &i.Latitude, &i.Longtitude, diff --git a/db/queries/accomodation-for-hospitality-panel.sql b/db/queries/accomodation-for-hospitality-panel.sql index 0341a927..afabb437 100644 --- a/db/queries/accomodation-for-hospitality-panel.sql +++ b/db/queries/accomodation-for-hospitality-panel.sql @@ -49,6 +49,7 @@ WHERE id = $1; UPDATE accomodation_details SET hostel_id = $2, + day_count = $3, payment_expires = NOW() + INTERVAL '30 minutes', payment_status = 'PENDING', updated_at = NOW() @@ -200,7 +201,7 @@ WHERE s.hospitality_id = $1; SELECT hm.id AS hostel_id, hm.hostel_name AS hostel_name, - hm.room_count AS available_rooms, + hm.room_count AS room_count, hm.is_male AS is_male, hm.latitude AS latitude, hm.longtitude AS longtitude, diff --git a/models/accommodation-admin.go b/models/accommodation-admin.go index 0f2117a1..7a951833 100644 --- a/models/accommodation-admin.go +++ b/models/accommodation-admin.go @@ -61,13 +61,13 @@ func (r UpdateHostelRequest) Validate() error { &r.Latitude, v.When(r.Latitude != "", v.Match(decimalRegex), - ).Else(v.Nil), + ), ), v.Field( &r.Longtitude, v.When(r.Longtitude != "", v.Match(decimalRegex), - ).Else(v.Nil), + ), ), v.Field(&r.MapUrl, is.URL), ) @@ -75,11 +75,13 @@ func (r UpdateHostelRequest) Validate() error { type AllotHostelRequest struct { HostelID string `json:"hostel_id"` + DayCount int32 `json:"day_count"` } func (r AllotHostelRequest) Validate() error { return v.ValidateStruct(&r, v.Field(&r.HostelID, v.Required, is.UUID), + v.Field(&r.DayCount, v.Required, v.Min(1), v.Max(4)), ) }