Skip to content

Commit

Permalink
Merge develop to staging (#137)
Browse files Browse the repository at this point in the history
Co-authored-by: José Fagundes <fagundesjg@outlook.com>
  • Loading branch information
rhuam and fagundesjg authored May 18, 2024
1 parent 7a9aea7 commit 48480b6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This is an empty migration.

CREATE EXTENSION IF NOT EXISTS unaccent;
6 changes: 6 additions & 0 deletions prisma/migrations/20240517192040_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- CreateEnum
CREATE TYPE "ShelterCategory" AS ENUM ('Shelter', 'DistributionCenter');

-- AlterTable
ALTER TABLE "shelters" ADD COLUMN "actived" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN "category" "ShelterCategory" NOT NULL DEFAULT 'Shelter';
29 changes: 18 additions & 11 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ enum AccessLevel {
Admin
}

enum ShelterCategory {
Shelter
DistributionCenter
}

model User {
id String @id @default(uuid())
name String
Expand Down Expand Up @@ -85,25 +90,27 @@ model Supply {
}

model Shelter {
id String @id @default(uuid())
name String @unique
pix String? @unique
id String @id @default(uuid())
name String @unique
pix String? @unique
address String
street String?
neighbourhood String?
city String?
streetNumber String? @map("street_number")
zipCode String? @map("zip_code")
petFriendly Boolean? @map("pet_friendly")
shelteredPeople Int? @map("sheltered_people")
streetNumber String? @map("street_number")
zipCode String? @map("zip_code")
petFriendly Boolean? @map("pet_friendly")
shelteredPeople Int? @map("sheltered_people")
capacity Int?
contact String?
prioritySum Int @default(value: 0) @map("priority_sum")
prioritySum Int @default(value: 0) @map("priority_sum")
latitude Float?
longitude Float?
verified Boolean @default(value: false)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
verified Boolean @default(value: false)
category ShelterCategory @default(value: Shelter)
actived Boolean @default(value: true)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
shelterManagers ShelterManagers[]
shelterSupplies ShelterSupply[]
Expand Down
34 changes: 17 additions & 17 deletions src/shelter/ShelterSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ShelterSearch {
) {
this.prismaService = prismaService;
this.formProps = { ...props };
this.getQuery = this.getQuery.bind(this);
}

priority(supplyIds: string[] = []): Prisma.ShelterWhereInput {
Expand Down Expand Up @@ -102,23 +103,20 @@ class ShelterSearch {
};
}

get search(): Prisma.ShelterWhereInput[] {
if (!this.formProps.search) return [];
async getSearch(): Promise<Prisma.ShelterWhereInput> {
if (!this.formProps.search) return {};

return [
{
address: {
contains: this.formProps.search,
mode: 'insensitive',
},
},
{
name: {
contains: this.formProps.search,
mode: 'insensitive',
},
const search = `${this.formProps.search.toLowerCase()}`;

const results = await this.prismaService.$queryRaw<{ id: string }[]>(
Prisma.sql`SELECT id FROM shelters WHERE lower(unaccent(address)) LIKE '%' || unaccent(${search}) || '%' OR lower(unaccent(name)) LIKE '%' || unaccent(${search}) || '%';`,
);

return {
id: {
in: results.map((r) => r.id),
},
];
};
}

get cities(): Prisma.ShelterWhereInput {
Expand Down Expand Up @@ -150,13 +148,15 @@ class ShelterSearch {
};
}

get query(): Prisma.ShelterWhereInput {
async getQuery(): Promise<Prisma.ShelterWhereInput> {
if (Object.keys(this.formProps).length === 0) return {};

const search = await this.getSearch();
const queryData = {
AND: [
this.cities,
this.geolocation,
{ OR: this.search },
search,
{ OR: this.shelterStatus },
this.priority(this.formProps.supplyIds),
this.supplyCategoryIds(this.formProps.priority),
Expand Down
8 changes: 7 additions & 1 deletion src/shelter/shelter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class ShelterService implements OnModuleInit {
latitude: true,
longitude: true,
verified: true,
actived: true,
category: true,
shelterSupplies: {
select: {
priority: true,
Expand Down Expand Up @@ -125,7 +127,9 @@ export class ShelterService implements OnModuleInit {
search: searchQuery,
} = SearchSchema.parse(query);
const queryData = ShelterSearchPropsSchema.parse(qs.parse(searchQuery));
const { query: where } = new ShelterSearch(this.prismaService, queryData);
const { getQuery } = new ShelterSearch(this.prismaService, queryData);
const where = await getQuery();

const count = await this.prismaService.shelter.count({ where });

const take = perPage;
Expand Down Expand Up @@ -157,6 +161,8 @@ export class ShelterService implements OnModuleInit {
verified: true,
latitude: true,
longitude: true,
actived: true,
category: true,
createdAt: true,
updatedAt: true,
shelterSupplies: {
Expand Down

0 comments on commit 48480b6

Please sign in to comment.