Skip to content

Commit

Permalink
feat: [Contracts] API - Approve contract by NGO
Browse files Browse the repository at this point in the history
  • Loading branch information
radulescuandrew committed Sep 12, 2024
1 parent 902bc71 commit b4bd65d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
26 changes: 20 additions & 6 deletions backend/src/api/documents/document-contract.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
Get,
Param,
Patch,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { CreateDocumentContractUsecase } from 'src/usecases/documents/new_contracts/create-document-contract.usecase';
import { CreateDocumentContractDto } from './dto/create-document-contract.dto';
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';
Expand All @@ -13,6 +22,8 @@ import {
PaginatedPresenter,
} from 'src/infrastructure/presenters/generic-paginated.presenter';
import { GetManyDocumentContractsDto } from './dto/get-many-document-contracts.dto';
import { UuidValidationPipe } from 'src/infrastructure/pipes/uuid.pipe';
import { ApproveDocumentContractByNgoUsecase } from 'src/usecases/documents/new_contracts/approve-document-contract-by-ngo.usecase';

@ApiBearerAuth()
@UseGuards(WebJwtAuthGuard)
Expand All @@ -21,6 +32,7 @@ export class DocumentContractController {
constructor(
private readonly createDocumentContractUsecase: CreateDocumentContractUsecase,
private readonly getManyDocumentContractsUsecase: GetManyDocumentContractsUsecase,
private readonly approveDocumentContractByNgoUsecase: ApproveDocumentContractByNgoUsecase,
) {}

@Post()
Expand Down Expand Up @@ -56,9 +68,11 @@ export class DocumentContractController {
});
}

/* TODO: GET /documents/contracts/check?year={year}&documentNumber={documentNumber}
CHECK IF A CONTRACT ALREADY EXISTS FOR THE GIVEN YEAR AND DOCUMENT NUMBER IN THE SAME ORGANIZATION
RETURN TRUE IF IT EXISTS, FALSE OTHERWISE
USED TO PREVENT DUPLICATE DOCUMENT NUMBERS IN THE SAME YEAR
*/
@Patch(':id/approve')
async approveDocumentContract(
@Param('id', UuidValidationPipe) id: string,
@ExtractUser() { organizationId }: IAdminUserModel,
): Promise<void> {
await this.approveDocumentContractByNgoUsecase.execute(id, organizationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IDocumentContractListViewModel,
} from '../models/document-contract-list-view.model';
import { Pagination } from 'src/infrastructure/base/repository-with-pagination.class';
import { DocumentContractStatus } from '../enums/contract-status.enum';

@Injectable()
export class DocumentContractFacade {
Expand All @@ -21,6 +22,14 @@ export class DocumentContractFacade {
private readonly documentContractListViewRepository: DocumentContractListViewRepository,
) {}

async approveDocumentContractByNGO(
documentContractId: string,
): Promise<IDocumentContractModel> {
return this.documentContractRepository.update(documentContractId, {
status: DocumentContractStatus.PENDING_NGO_REPRESENTATIVE_SIGNATURE,
});
}

async create(
newDocumentContract: CreateDocumentContractOptions,
): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { ExceptionsService } from 'src/infrastructure/exceptions/exceptions.service';
import { DocumentContractStatus } from 'src/modules/documents/enums/contract-status.enum';
import { ContractExceptionMessages } from 'src/modules/documents/exceptions/contract.exceptions';
import { DocumentContractFacade } from 'src/modules/documents/services/document-contract.facade';

@Injectable()
export class ApproveDocumentContractByNgoUsecase {
constructor(
private readonly documentContractFacade: DocumentContractFacade,
private readonly exceptionService: ExceptionsService,
) {}

async execute(
documentContractId: string,
organizationId: string,
): Promise<void> {
const exists = await this.documentContractFacade.exists({
id: documentContractId,
organizationId,
status: DocumentContractStatus.PENDING_APPROVAL_NGO,
});

if (!exists) {
this.exceptionService.notFoundException(
ContractExceptionMessages.CONTRACT_002,
);
}
try {
await this.documentContractFacade.approveDocumentContractByNGO(
documentContractId,
);
} catch (error) {
this.exceptionService.internalServerErrorException({
message: `Error while approving the contract by NGO ${error?.message}`,
code_error: 'APPROVE_DOCUMENT_CONTRACT_BY_NGO_001',
});
}
}
}
2 changes: 2 additions & 0 deletions backend/src/usecases/use-case.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ import { RejectDocumentContractByVolunteerUsecase } from './documents/new_contra
import { GeneratePDFsUseCase } from './documents/new_contracts/generate-pdfs.usecase';
import { GetManyDocumentContractsByVolunteerUsecase } from './documents/new_contracts/get-many-document-contracts-by-volunteer.usecase';
import { GetOneDocumentContractForVolunteerUsecase } from './documents/new_contracts/get-one-document-contract-for-volunteer.usecase';
import { ApproveDocumentContractByNgoUsecase } from './documents/new_contracts/approve-document-contract-by-ngo.usecase';

const providers = [
// Organization
Expand Down Expand Up @@ -300,6 +301,7 @@ const providers = [
RejectDocumentContractByVolunteerUsecase,
GetManyDocumentContractsByVolunteerUsecase,
GetOneDocumentContractForVolunteerUsecase,
ApproveDocumentContractByNgoUsecase,
// Notifications
UpdateSettingsUsecase,
// Testing PDFs
Expand Down

0 comments on commit b4bd65d

Please sign in to comment.