Skip to content

Commit 2f50428

Browse files
authored
Feat: carrinho de doações (#182)
1 parent 55a1a9a commit 2f50428

24 files changed

+1216
-169
lines changed

docs/sos-rs.insomnia.json

Lines changed: 669 additions & 1 deletion
Large diffs are not rendered by default.

prisma/dev_dump.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ INSERT INTO public.partners VALUES ('33fc6b9c-e9dd-446d-b0a1-f66dd8a45cda', 'Pai
9393
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: doadmin
9494
--
9595

96-
INSERT INTO public.users VALUES ('e0306bc0-8c29-429a-bbd2-384f48d4f993', 'Dinho', '31996675945', '$2b$10$V4hFTbT7MrskROc4TI2lNe6gAd0g7U1niziAPycFueLhPJRFIfoGm', '31996675945', '2024-05-05T22:04:59.468Z', '2024-05-09T17:56:55.037Z', 'Admin', 'Duarte');
96+
INSERT INTO public.users VALUES ('e0306bc0-8c29-429a-bbd2-384f48d4f993', 'Administrador', '31999999999', '$2b$10$V4hFTbT7MrskROc4TI2lNe6gAd0g7U1niziAPycFueLhPJRFIfoGm', '31999999999', '2024-05-05T22:04:59.468Z', '2024-05-09T17:56:55.037Z', 'Admin', 'Web');
9797
INSERT INTO public.users VALUES ('e82f476a-1574-4dd2-a4c4-1c1f0117db12', 'Rhuam', '51992047974', '$2b$10$V4hFTbT7MrskROc4TI2lNe6gAd0g7U1niziAPycFueLhPJRFIfoGm', '51992047974', '2024-05-05T22:06:11.390Z', '2024-05-16T18:52:36.650Z', 'Admin', 'Estevam');
9898

9999

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- CreateEnum
2+
CREATE TYPE "SupplyMeasure" AS ENUM ('Unit', 'Kg', 'Litters', 'Box', 'Piece');
3+
4+
-- CreateEnum
5+
CREATE TYPE "DonationOrderStatus" AS ENUM ('Pending', 'Canceled', 'Complete');
6+
7+
-- AlterTable
8+
ALTER TABLE "supplies" ADD COLUMN "measure" "SupplyMeasure" NOT NULL DEFAULT 'Unit';
9+
10+
-- CreateTable
11+
CREATE TABLE "donation_order_supplies" (
12+
"id" TEXT NOT NULL,
13+
"donation_order_id" TEXT NOT NULL,
14+
"supply_id" TEXT NOT NULL,
15+
"quantity" INTEGER NOT NULL,
16+
"created_at" VARCHAR(32) NOT NULL,
17+
"updated_at" VARCHAR(32),
18+
19+
CONSTRAINT "donation_order_supplies_pkey" PRIMARY KEY ("id")
20+
);
21+
22+
-- CreateTable
23+
CREATE TABLE "donation_orders" (
24+
"id" TEXT NOT NULL,
25+
"user_id" TEXT NOT NULL,
26+
"shelter_id" TEXT NOT NULL,
27+
"status" "DonationOrderStatus" NOT NULL DEFAULT 'Pending',
28+
"created_at" VARCHAR(32) NOT NULL,
29+
"updated_at" VARCHAR(32),
30+
31+
CONSTRAINT "donation_orders_pkey" PRIMARY KEY ("id")
32+
);
33+
34+
-- AddForeignKey
35+
ALTER TABLE "donation_order_supplies" ADD CONSTRAINT "donation_order_supplies_supply_id_fkey" FOREIGN KEY ("supply_id") REFERENCES "supplies"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
36+
37+
-- AddForeignKey
38+
ALTER TABLE "donation_order_supplies" ADD CONSTRAINT "donation_order_supplies_donation_order_id_fkey" FOREIGN KEY ("donation_order_id") REFERENCES "donation_orders"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
39+
40+
-- AddForeignKey
41+
ALTER TABLE "donation_orders" ADD CONSTRAINT "donation_orders_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
42+
43+
-- AddForeignKey
44+
ALTER TABLE "donation_orders" ADD CONSTRAINT "donation_orders_shelter_id_fkey" FOREIGN KEY ("shelter_id") REFERENCES "shelters"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ enum ShelterCategory {
1919
DistributionCenter
2020
}
2121

22+
enum SupplyMeasure {
23+
Unit
24+
Kg
25+
Litters
26+
Box
27+
Piece
28+
}
29+
30+
enum DonationOrderStatus {
31+
Pending
32+
Canceled
33+
Complete
34+
}
35+
2236
model User {
2337
id String @id @default(uuid())
2438
name String
@@ -33,6 +47,7 @@ model User {
3347
sessions Session[]
3448
shelterManagers ShelterManagers[]
3549
suppliesHistory SupplyHistory[]
50+
donationOrders DonationOrder[]
3651
3752
@@map("users")
3853
}
@@ -78,15 +93,17 @@ model ShelterSupply {
7893
}
7994

8095
model Supply {
81-
id String @id @default(uuid())
82-
supplyCategoryId String @map("supply_category_id")
96+
id String @id @default(uuid())
97+
supplyCategoryId String @map("supply_category_id")
8398
name String
84-
createdAt String @map("created_at") @db.VarChar(32)
85-
updatedAt String? @map("updated_at") @db.VarChar(32)
99+
measure SupplyMeasure @default(value: Unit)
100+
createdAt String @map("created_at") @db.VarChar(32)
101+
updatedAt String? @map("updated_at") @db.VarChar(32)
86102
87-
supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id])
88-
shelterSupplies ShelterSupply[]
89-
supplyHistories SupplyHistory[]
103+
supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id])
104+
shelterSupplies ShelterSupply[]
105+
supplyHistories SupplyHistory[]
106+
DonationOrderSupply DonationOrderSupply[]
90107
91108
@@map("supplies")
92109
}
@@ -117,6 +134,7 @@ model Shelter {
117134
shelterManagers ShelterManagers[]
118135
shelterSupplies ShelterSupply[]
119136
supplyHistories SupplyHistory[]
137+
DonationOrder DonationOrder[]
120138
121139
@@map("shelters")
122140
}
@@ -175,3 +193,32 @@ model SupplyHistory {
175193
176194
@@map("supplies_history")
177195
}
196+
197+
model DonationOrderSupply {
198+
id String @id @default(uuid())
199+
donationOrderId String @map("donation_order_id")
200+
supplyId String @map("supply_id")
201+
quantity Int
202+
createdAt String @map("created_at") @db.VarChar(32)
203+
updatedAt String? @map("updated_at") @db.VarChar(32)
204+
205+
supply Supply @relation(fields: [supplyId], references: [id])
206+
donationOrder DonationOrder @relation(fields: [donationOrderId], references: [id])
207+
208+
@@map("donation_order_supplies")
209+
}
210+
211+
model DonationOrder {
212+
id String @id @default(uuid())
213+
userId String @map("user_id")
214+
shelterId String @map("shelter_id")
215+
status DonationOrderStatus @default(value: Pending)
216+
createdAt String @map("created_at") @db.VarChar(32)
217+
updatedAt String? @map("updated_at") @db.VarChar(32)
218+
219+
user User @relation(fields: [userId], references: [id])
220+
shelter Shelter @relation(fields: [shelterId], references: [id])
221+
donationOrderSupplies DonationOrderSupply[]
222+
223+
@@map("donation_orders")
224+
}

src/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import { SupplyCategoriesModule } from './supply-categories/supply-categories.mo
1212
import { ShelterManagersModule } from './shelter-managers/shelter-managers.module';
1313
import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
1414
import { PartnersModule } from './partners/partners.module';
15-
import { DashboardModule } from './modules/dashboard/dashboard.module';
15+
import { DashboardModule } from './dashboard/dashboard.module';
1616
import { SupportersModule } from './supporters/supporters.module';
1717
import { SuppliesHistoryModule } from './supplies-history/supplies-history.module';
18+
import { DonationOrderModule } from './donation-order/donation-order.module';
1819

1920
@Module({
2021
imports: [
@@ -30,6 +31,7 @@ import { SuppliesHistoryModule } from './supplies-history/supplies-history.modul
3031
DashboardModule,
3132
SupportersModule,
3233
SuppliesHistoryModule,
34+
DonationOrderModule,
3335
],
3436
controllers: [],
3537
providers: [

src/modules/dashboard/dashboard.controller.spec.ts renamed to src/dashboard/dashboard.controller.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2+
23
import { DashboardController } from './dashboard.controller';
34
import { DashboardService } from './dashboard.service';
5+
import { PrismaService } from '../../prisma/prisma.service';
6+
import { PrismaModule } from '../../prisma/prisma.module';
47

58
describe('DashboardController', () => {
69
let controller: DashboardController;
710

811
beforeEach(async () => {
912
const module: TestingModule = await Test.createTestingModule({
13+
imports: [PrismaModule],
1014
controllers: [DashboardController],
1115
providers: [DashboardService],
12-
}).compile();
16+
})
17+
.useMocker((token) => {
18+
if (token === PrismaService) {
19+
return {};
20+
}
21+
})
22+
.compile();
1323

1424
controller = module.get<DashboardController>(DashboardController);
1525
});
File renamed without changes.
File renamed without changes.

src/modules/dashboard/dashboard.service.spec.ts renamed to src/dashboard/dashboard.service.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2+
23
import { DashboardService } from './dashboard.service';
4+
import { PrismaService } from '../../prisma/prisma.service';
5+
import { PrismaModule } from '../../prisma/prisma.module';
36

47
describe('DashboardService', () => {
58
let service: DashboardService;
69

710
beforeEach(async () => {
811
const module: TestingModule = await Test.createTestingModule({
12+
imports: [PrismaModule],
913
providers: [DashboardService],
10-
}).compile();
14+
})
15+
.useMocker((token) => {
16+
if (token === PrismaService) {
17+
return {};
18+
}
19+
})
20+
.compile();
1121

1222
service = module.get<DashboardService>(DashboardService);
1323
});

src/modules/dashboard/dashboard.service.ts renamed to src/dashboard/dashboard.service.ts

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class DashboardService {
4646
priority: true,
4747
supply: {
4848
select: {
49+
measure: true,
4950
supplyCategory: {
5051
select: {
5152
name: true,
@@ -58,34 +59,35 @@ export class DashboardService {
5859
},
5960
});
6061

61-
const categoriesWithPriorities = await this.prismaService.supplyCategory.findMany({
62-
select: {
63-
id: true,
64-
name: true,
65-
supplies: {
66-
select: {
67-
shelterSupplies: {
68-
select: {
69-
priority: true,
70-
shelterId: true
71-
}
72-
}
73-
}
74-
}
75-
}
76-
});
77-
78-
const result = categoriesWithPriorities.map(category => {
62+
const categoriesWithPriorities =
63+
await this.prismaService.supplyCategory.findMany({
64+
select: {
65+
id: true,
66+
name: true,
67+
supplies: {
68+
select: {
69+
shelterSupplies: {
70+
select: {
71+
priority: true,
72+
shelterId: true,
73+
},
74+
},
75+
},
76+
},
77+
},
78+
});
79+
80+
const result = categoriesWithPriorities.map((category) => {
7981
const priorityCounts = {
8082
priority100: 0,
8183
priority10: 0,
8284
priority1: 0,
8385
};
84-
86+
8587
const countedShelters = new Set();
86-
87-
category.supplies.forEach(supply => {
88-
supply.shelterSupplies.forEach(shelterSupply => {
88+
89+
category.supplies.forEach((supply) => {
90+
supply.shelterSupplies.forEach((shelterSupply) => {
8991
if (!countedShelters.has(shelterSupply.shelterId)) {
9092
switch (shelterSupply.priority) {
9193
case 100:
@@ -124,30 +126,40 @@ export class DashboardService {
124126
}
125127
}, 0);
126128

127-
const numSheltersAvailable = allShelters.filter(shelter => {
128-
if (shelter.actived && shelter.capacity !== null && shelter.capacity > 0) {
129-
return (shelter.shelteredPeople ?? 0) < shelter.capacity;
129+
const numSheltersAvailable = allShelters.filter((shelter) => {
130+
if (
131+
shelter.actived &&
132+
shelter.capacity !== null &&
133+
shelter.capacity > 0
134+
) {
135+
return (shelter.shelteredPeople ?? 0) < shelter.capacity;
130136
}
131-
return false;
132-
}).length;
137+
return false;
138+
}).length;
133139

134140
const numSheltersFull = allShelters.reduce((count, shelter) => {
135-
if (shelter.actived && shelter.capacity !== null && shelter.capacity > 0) {
136-
if ((shelter.shelteredPeople ?? 0) >= shelter.capacity) {
137-
return count + 1;
138-
}
141+
if (
142+
shelter.actived &&
143+
shelter.capacity !== null &&
144+
shelter.capacity > 0
145+
) {
146+
if ((shelter.shelteredPeople ?? 0) >= shelter.capacity) {
147+
return count + 1;
148+
}
139149
}
140150
return count;
141151
}, 0);
142152

143153
const shelterWithoutInformation = allShelters.reduce((count, shelter) => {
144-
if (shelter.shelteredPeople === null || shelter.shelteredPeople === undefined) {
145-
return count + 1;
154+
if (
155+
shelter.shelteredPeople === null ||
156+
shelter.shelteredPeople === undefined
157+
) {
158+
return count + 1;
146159
}
147160
return count;
148161
}, 0);
149-
150-
162+
151163
return {
152164
allShelters: allShelters.length,
153165
allPeopleSheltered: allPeopleSheltered,

0 commit comments

Comments
 (0)