Skip to content
Merged
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
670 changes: 669 additions & 1 deletion docs/sos-rs.insomnia.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion prisma/dev_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ INSERT INTO public.partners VALUES ('33fc6b9c-e9dd-446d-b0a1-f66dd8a45cda', 'Pai
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: doadmin
--

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');
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');
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');


Expand Down
44 changes: 44 additions & 0 deletions prisma/migrations/20240528182902_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- CreateEnum
CREATE TYPE "SupplyMeasure" AS ENUM ('Unit', 'Kg', 'Litters', 'Box', 'Piece');

-- CreateEnum
CREATE TYPE "DonationOrderStatus" AS ENUM ('Pending', 'Canceled', 'Complete');

-- AlterTable
ALTER TABLE "supplies" ADD COLUMN "measure" "SupplyMeasure" NOT NULL DEFAULT 'Unit';

-- CreateTable
CREATE TABLE "donation_order_supplies" (
"id" TEXT NOT NULL,
"donation_order_id" TEXT NOT NULL,
"supply_id" TEXT NOT NULL,
"quantity" INTEGER NOT NULL,
"created_at" VARCHAR(32) NOT NULL,
"updated_at" VARCHAR(32),

CONSTRAINT "donation_order_supplies_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "donation_orders" (
"id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"shelter_id" TEXT NOT NULL,
"status" "DonationOrderStatus" NOT NULL DEFAULT 'Pending',
"created_at" VARCHAR(32) NOT NULL,
"updated_at" VARCHAR(32),

CONSTRAINT "donation_orders_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
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;

-- AddForeignKey
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;

-- AddForeignKey
ALTER TABLE "donation_orders" ADD CONSTRAINT "donation_orders_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "donation_orders" ADD CONSTRAINT "donation_orders_shelter_id_fkey" FOREIGN KEY ("shelter_id") REFERENCES "shelters"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
61 changes: 54 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ enum ShelterCategory {
DistributionCenter
}

enum SupplyMeasure {
Unit
Kg
Litters
Box
Piece
}

enum DonationOrderStatus {
Pending
Canceled
Complete
}

model User {
id String @id @default(uuid())
name String
Expand All @@ -33,6 +47,7 @@ model User {
sessions Session[]
shelterManagers ShelterManagers[]
suppliesHistory SupplyHistory[]
donationOrders DonationOrder[]

@@map("users")
}
Expand Down Expand Up @@ -78,15 +93,17 @@ model ShelterSupply {
}

model Supply {
id String @id @default(uuid())
supplyCategoryId String @map("supply_category_id")
id String @id @default(uuid())
supplyCategoryId String @map("supply_category_id")
name String
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
measure SupplyMeasure @default(value: Unit)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)

supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id])
shelterSupplies ShelterSupply[]
supplyHistories SupplyHistory[]
supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id])
shelterSupplies ShelterSupply[]
supplyHistories SupplyHistory[]
DonationOrderSupply DonationOrderSupply[]

@@map("supplies")
}
Expand Down Expand Up @@ -117,6 +134,7 @@ model Shelter {
shelterManagers ShelterManagers[]
shelterSupplies ShelterSupply[]
supplyHistories SupplyHistory[]
DonationOrder DonationOrder[]

@@map("shelters")
}
Expand Down Expand Up @@ -175,3 +193,32 @@ model SupplyHistory {

@@map("supplies_history")
}

model DonationOrderSupply {
id String @id @default(uuid())
donationOrderId String @map("donation_order_id")
supplyId String @map("supply_id")
quantity Int
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)

supply Supply @relation(fields: [supplyId], references: [id])
donationOrder DonationOrder @relation(fields: [donationOrderId], references: [id])

@@map("donation_order_supplies")
}

model DonationOrder {
id String @id @default(uuid())
userId String @map("user_id")
shelterId String @map("shelter_id")
status DonationOrderStatus @default(value: Pending)
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)

user User @relation(fields: [userId], references: [id])
shelter Shelter @relation(fields: [shelterId], references: [id])
donationOrderSupplies DonationOrderSupply[]

@@map("donation_orders")
}
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { SupplyCategoriesModule } from './supply-categories/supply-categories.mo
import { ShelterManagersModule } from './shelter-managers/shelter-managers.module';
import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
import { PartnersModule } from './partners/partners.module';
import { DashboardModule } from './modules/dashboard/dashboard.module';
import { DashboardModule } from './dashboard/dashboard.module';
import { SupportersModule } from './supporters/supporters.module';
import { SuppliesHistoryModule } from './supplies-history/supplies-history.module';
import { DonationOrderModule } from './donation-order/donation-order.module';

@Module({
imports: [
Expand All @@ -30,6 +31,7 @@ import { SuppliesHistoryModule } from './supplies-history/supplies-history.modul
DashboardModule,
SupportersModule,
SuppliesHistoryModule,
DonationOrderModule,
],
controllers: [],
providers: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Test, TestingModule } from '@nestjs/testing';

import { DashboardController } from './dashboard.controller';
import { DashboardService } from './dashboard.service';
import { PrismaService } from '../../prisma/prisma.service';
import { PrismaModule } from '../../prisma/prisma.module';

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

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [PrismaModule],
controllers: [DashboardController],
providers: [DashboardService],
}).compile();
})
.useMocker((token) => {
if (token === PrismaService) {
return {};
}
})
.compile();

controller = module.get<DashboardController>(DashboardController);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Test, TestingModule } from '@nestjs/testing';

import { DashboardService } from './dashboard.service';
import { PrismaService } from '../../prisma/prisma.service';
import { PrismaModule } from '../../prisma/prisma.module';

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

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [PrismaModule],
providers: [DashboardService],
}).compile();
})
.useMocker((token) => {
if (token === PrismaService) {
return {};
}
})
.compile();

service = module.get<DashboardService>(DashboardService);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class DashboardService {
priority: true,
supply: {
select: {
measure: true,
supplyCategory: {
select: {
name: true,
Expand All @@ -58,34 +59,35 @@ export class DashboardService {
},
});

const categoriesWithPriorities = await this.prismaService.supplyCategory.findMany({
select: {
id: true,
name: true,
supplies: {
select: {
shelterSupplies: {
select: {
priority: true,
shelterId: true
}
}
}
}
}
});

const result = categoriesWithPriorities.map(category => {
const categoriesWithPriorities =
await this.prismaService.supplyCategory.findMany({
select: {
id: true,
name: true,
supplies: {
select: {
shelterSupplies: {
select: {
priority: true,
shelterId: true,
},
},
},
},
},
});

const result = categoriesWithPriorities.map((category) => {
const priorityCounts = {
priority100: 0,
priority10: 0,
priority1: 0,
};

const countedShelters = new Set();
category.supplies.forEach(supply => {
supply.shelterSupplies.forEach(shelterSupply => {

category.supplies.forEach((supply) => {
supply.shelterSupplies.forEach((shelterSupply) => {
if (!countedShelters.has(shelterSupply.shelterId)) {
switch (shelterSupply.priority) {
case 100:
Expand Down Expand Up @@ -124,30 +126,40 @@ export class DashboardService {
}
}, 0);

const numSheltersAvailable = allShelters.filter(shelter => {
if (shelter.actived && shelter.capacity !== null && shelter.capacity > 0) {
return (shelter.shelteredPeople ?? 0) < shelter.capacity;
const numSheltersAvailable = allShelters.filter((shelter) => {
if (
shelter.actived &&
shelter.capacity !== null &&
shelter.capacity > 0
) {
return (shelter.shelteredPeople ?? 0) < shelter.capacity;
}
return false;
}).length;
return false;
}).length;

const numSheltersFull = allShelters.reduce((count, shelter) => {
if (shelter.actived && shelter.capacity !== null && shelter.capacity > 0) {
if ((shelter.shelteredPeople ?? 0) >= shelter.capacity) {
return count + 1;
}
if (
shelter.actived &&
shelter.capacity !== null &&
shelter.capacity > 0
) {
if ((shelter.shelteredPeople ?? 0) >= shelter.capacity) {
return count + 1;
}
}
return count;
}, 0);

const shelterWithoutInformation = allShelters.reduce((count, shelter) => {
if (shelter.shelteredPeople === null || shelter.shelteredPeople === undefined) {
return count + 1;
if (
shelter.shelteredPeople === null ||
shelter.shelteredPeople === undefined
) {
return count + 1;
}
return count;
}, 0);



return {
allShelters: allShelters.length,
allPeopleSheltered: allPeopleSheltered,
Expand Down
30 changes: 30 additions & 0 deletions src/donation-order/donation-order.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Test, TestingModule } from '@nestjs/testing';

import { DonationOrderController } from './donation-order.controller';
import { PrismaService } from '../prisma/prisma.service';
import { PrismaModule } from '../prisma/prisma.module';
import { DonationOrderService } from './donation-order.service';

describe('DonationOrderController', () => {
let controller: DonationOrderController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [PrismaModule],
providers: [DonationOrderService],
controllers: [DonationOrderController],
})
.useMocker((token) => {
if (token === PrismaService) {
return {};
}
})
.compile();

controller = module.get<DonationOrderController>(DonationOrderController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
Loading