Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions prisma/migrations/20240523235040_sheltered_pets/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "shelters" ADD COLUMN "pet_capacity" INTEGER,
ADD COLUMN "sheltered_pets" INTEGER;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ model Shelter {
petFriendly Boolean? @map("pet_friendly")
shelteredPeople Int? @map("sheltered_people")
capacity Int?
shelteredPets Int? @map("sheltered_pets")
petCapacity Int? @map("pet_capacity")
contact String?
prioritySum Int @default(value: 0) @map("priority_sum")
latitude Float?
Expand Down
4 changes: 4 additions & 0 deletions src/shelter/shelter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export class ShelterService implements OnModuleInit {
pix: true,
shelteredPeople: true,
capacity: true,
shelteredPets: true,
petCapacity: true,
contact: shouldShowContact,
petFriendly: true,
prioritySum: true,
Expand Down Expand Up @@ -155,8 +157,10 @@ export class ShelterService implements OnModuleInit {
streetNumber: true,
zipCode: true,
capacity: true,
petCapacity: true,
petFriendly: true,
shelteredPeople: true,
shelteredPets: true,
prioritySum: true,
verified: true,
latitude: true,
Expand Down
28 changes: 25 additions & 3 deletions src/shelter/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export interface DefaultSupplyProps {
supply: string;
}

const petFriendlyRefinement = function(obj, ctx) {
if(!obj.petFriendly) {
if(obj.petCapacity) ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Shelter is not pet friendly',
path: ['petCapacity']
});

if(obj.shelteredPets) ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Shelter is not pet friendly',
path: ['shelteredPets']
});
}
};

const ShelterSchema = z.object({
id: z.string(),
name: z.string().transform(capitalize),
Expand All @@ -20,9 +36,11 @@ const ShelterSchema = z.object({
zipCode: z.string().nullable().optional(),
petFriendly: z.boolean().nullable().optional(),
shelteredPeople: z.number().min(0).nullable().optional(),
shelteredPets: z.number().min(0).nullable().optional(),
latitude: z.number().nullable().optional(),
longitude: z.number().nullable().optional(),
capacity: z.number().min(0).nullable().optional(),
petCapacity: z.number().min(0).nullable().optional(),
contact: z.string().nullable().optional(),
verified: z.boolean(),
createdAt: z.string(),
Expand All @@ -34,20 +52,24 @@ const CreateShelterSchema = ShelterSchema.omit({
createdAt: true,
updatedAt: true,
verified: true,
});
}).superRefine(petFriendlyRefinement);

const UpdateShelterSchema = ShelterSchema.pick({
petFriendly: true,
shelteredPeople: true,
}).partial();
petCapacity: true,
shelteredPets: true
}).partial()
.superRefine(petFriendlyRefinement);

const FullUpdateShelterSchema = ShelterSchema.omit({
id: true,
createdAt: true,
updatedAt: true,
})
.partial()
.transform((args) => removeEmptyStrings<typeof args>(args));
.transform((args) => removeEmptyStrings<typeof args>(args))
.superRefine(petFriendlyRefinement);

export {
ShelterSchema,
Expand Down