Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new route and service to see shelter supply changelog #147

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions prisma/migrations/20240519130138_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- CreateTable
CREATE TABLE "shelter_supply_logs" (
"id" TEXT NOT NULL,
"shelter_suply_shelter_id" TEXT NOT NULL,
"shelter_suply_supply_id" TEXT NOT NULL,
"priority" INTEGER,
"quantity" INTEGER,
"created_at" VARCHAR(32) NOT NULL,

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

-- CreateIndex
CREATE INDEX "shelter_supply_logs_shelter_suply_shelter_id_shelter_suply__idx" ON "shelter_supply_logs"("shelter_suply_shelter_id", "shelter_suply_supply_id");

-- AddForeignKey
ALTER TABLE "shelter_supply_logs" ADD CONSTRAINT "shelter_supply_logs_shelter_suply_shelter_id_shelter_suply_fkey" FOREIGN KEY ("shelter_suply_shelter_id", "shelter_suply_supply_id") REFERENCES "shelter_supplies"("shelter_id", "supply_id") ON DELETE RESTRICT ON UPDATE CASCADE;
16 changes: 16 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,26 @@ model ShelterSupply {
shelter Shelter @relation(fields: [shelterId], references: [id])
supply Supply @relation(fields: [supplyId], references: [id])

ShelterSupplyLog ShelterSupplyLog[]

@@id([shelterId, supplyId])
@@map("shelter_supplies")
}

model ShelterSupplyLog {
id String @id @default(uuid())
shelterSupplyShelterId String @map("shelter_suply_shelter_id")
shelterSupplySupplyId String @map("shelter_suply_supply_id")
priority Int?
quantity Int?
createdAt String @map("created_at") @db.VarChar(32)

shelterSupply ShelterSupply @relation(fields: [shelterSupplyShelterId, shelterSupplySupplyId], references: [shelterId, supplyId])

@@index([shelterSupplyShelterId, shelterSupplySupplyId])
@@map("shelter_supply_logs")
}

model Supply {
id String @id @default(uuid())
supplyCategoryId String @map("supply_category_id")
Expand Down
14 changes: 8 additions & 6 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';

import { PrismaModule } from './prisma/prisma.module';
import { ShelterModule } from './shelter/shelter.module';
import { SupplyModule } from './supply/supply.module';
import { ServerResponseInterceptor } from './interceptors';
import { LoggingMiddleware } from './middlewares/logging.middleware';
import { UsersModule } from './users/users.module';
import { PartnersModule } from './partners/partners.module';
import { PrismaModule } from './prisma/prisma.module';
import { SessionsModule } from './sessions/sessions.module';
import { SupplyCategoriesModule } from './supply-categories/supply-categories.module';
import { ShelterManagersModule } from './shelter-managers/shelter-managers.module';
import { ShelterSupplyLogsModule } from './shelter-supply-logs/shelter-supply-log.module';
import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
import { PartnersModule } from './partners/partners.module';
import { ShelterModule } from './shelter/shelter.module';
import { SupplyCategoriesModule } from './supply-categories/supply-categories.module';
import { SupplyModule } from './supply/supply.module';
import { SupportersModule } from './supporters/supporters.module';
import { UsersModule } from './users/users.module';

@Module({
imports: [
Expand All @@ -24,6 +25,7 @@ import { SupportersModule } from './supporters/supporters.module';
SupplyCategoriesModule,
ShelterManagersModule,
ShelterSupplyModule,
ShelterSupplyLogsModule,
PartnersModule,
SupportersModule,
],
Expand Down
45 changes: 45 additions & 0 deletions src/shelter-supply-logs/shelter-supply-log.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Controller,
Get,
HttpException,
Logger,
Param,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { DistributionCenterGuard } from '@/guards/distribution-center.guard';
import { ServerResponse } from '../utils';
import { ShelterSupplyLogsService } from './shelter-supply-log.service';

@ApiTags('Logs do suprimento de abrigos')
@Controller('shelter/supplies/logs')
export class ShelterSupplyLogsController {
private logger = new Logger(ShelterSupplyLogsController.name);

constructor(
private readonly shelterSupplyLogsService: ShelterSupplyLogsService,
) {}

@Get(':shelterId/:supplyId')
@UseGuards(DistributionCenterGuard)
async getLogs(
@Param('shelterId') shelterId: string,
@Param('supplyId') supplyId: string,
) {
try {
const data = await this.shelterSupplyLogsService.getLogs(
shelterId,
supplyId,
);
return new ServerResponse(
200,
'Successfully get shelter supplies logs',
data,
);
} catch (err: any) {
this.logger.error(`Failed to get shelter supplies: ${err}`);
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}
}
12 changes: 12 additions & 0 deletions src/shelter-supply-logs/shelter-supply-log.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';

import { PrismaModule } from '../prisma/prisma.module';
import { ShelterSupplyLogsController } from './shelter-supply-log.controller';
import { ShelterSupplyLogsService } from './shelter-supply-log.service';

@Module({
imports: [PrismaModule],
providers: [ShelterSupplyLogsService],
controllers: [ShelterSupplyLogsController],
})
export class ShelterSupplyLogsModule {}
39 changes: 39 additions & 0 deletions src/shelter-supply-logs/shelter-supply-log.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@nestjs/common';

import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class ShelterSupplyLogsService {
constructor(private readonly prismaService: PrismaService) {}

async getLogs(shelterId: string, supplyId: string) {
return await this.prismaService.shelterSupplyLog.findMany({
where: {
shelterSupplyShelterId: shelterId,
shelterSupplySupplyId: supplyId,
},
select: {
priority: true,
quantity: true,
createdAt: true,
},
});
}

async addLog(
shelterId: string,
supplyId: string,
priority: number,
quantity: number,
): Promise<void> {
await await this.prismaService.shelterSupplyLog.create({
data: {
shelterSupplyShelterId: shelterId,
shelterSupplySupplyId: supplyId,
priority,
quantity,
createdAt: new Date().toISOString(),
},
});
}
}
29 changes: 29 additions & 0 deletions src/shelter-supply-logs/shelter-supply.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from 'src/prisma/prisma.service';
import { ShelterSupplyLogsController } from './shelter-supply-log.controller';
import { ShelterSupplyLogsService } from './shelter-supply-log.service';

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

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

controller = module.get<ShelterSupplyLogsController>(
ShelterSupplyLogsController,
);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
25 changes: 25 additions & 0 deletions src/shelter-supply-logs/shelter-supply.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from 'src/prisma/prisma.service';
import { ShelterSupplyLogsService } from './shelter-supply-log.service';

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

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

service = module.get<ShelterSupplyLogsService>(ShelterSupplyLogsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
4 changes: 2 additions & 2 deletions src/shelter-supply/shelter-supply.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';

import { ShelterSupplyService } from './shelter-supply.service';
import { ShelterSupplyController } from './shelter-supply.controller';
import { PrismaModule } from '../prisma/prisma.module';
import { ShelterSupplyController } from './shelter-supply.controller';
import { ShelterSupplyService } from './shelter-supply.service';

@Module({
imports: [PrismaModule],
Expand Down
13 changes: 11 additions & 2 deletions src/shelter-supply/shelter-supply.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { z } from 'zod';
import { Injectable } from '@nestjs/common';
import { z } from 'zod';

import { PrismaService } from '../prisma/prisma.service';
import { SupplyPriority } from '../supply/types';
import {
CreateShelterSupplySchema,
UpdateManyShelterSupplySchema,
UpdateShelterSupplySchema,
} from './types';
import { SupplyPriority } from '../supply/types';

@Injectable()
export class ShelterSupplyService {
Expand Down Expand Up @@ -66,6 +66,15 @@ export class ShelterSupplyService {
priority,
);
}
await this.prismaService.shelterSupplyLog.create({
data: {
shelterSupplyShelterId: where.shelterId,
shelterSupplySupplyId: where.supplyId,
priority,
quantity,
createdAt: new Date().toISOString(),
},
});

await this.prismaService.shelterSupply.update({
where: {
Expand Down