Skip to content

Commit

Permalink
fix: [Contracts] Delete file from S3 when the contract is deleted, in…
Browse files Browse the repository at this point in the history
…validate query in FE
  • Loading branch information
radulescuandrew committed Oct 1, 2024
1 parent 2b4d21e commit da540ed
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class DocumentSignatureRepository {
return signature.id;
}

async delete(id: string): Promise<void> {
await this.signatureRepository.delete({ id });
}

async findOne(
options: FindOneSignatureOptions,
): Promise<DocumentSignatureEntity> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export class DocumentSignatureFacade {
async create(newSignature: CreateSignatureOptions): Promise<string> {
return this.documentSignatureRepository.create(newSignature);
}

async delete(id: string): Promise<void> {
return this.documentSignatureRepository.delete(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { Injectable, Logger } from '@nestjs/common';
import { JSONStringifyError } from 'src/common/helpers/utils';
import { IUseCaseService } from 'src/common/interfaces/use-case-service.interface';
import { ExceptionsService } from 'src/infrastructure/exceptions/exceptions.service';
import { S3Service } from 'src/infrastructure/providers/s3/module/s3.service';
import { ActionsArchiveFacade } from 'src/modules/actions-archive/actions-archive.facade';
import { TrackedEventName } from 'src/modules/actions-archive/enums/action-resource-types.enum';
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';
import { IAdminUserModel } from 'src/modules/user/models/admin-user.model';
import * as Sentry from '@sentry/nestjs';
import { DocumentSignatureFacade } from 'src/modules/documents/services/document-signature.facade';

@Injectable()
export class DeleteDocumentContractUsecase implements IUseCaseService<string> {
Expand All @@ -16,6 +19,8 @@ export class DeleteDocumentContractUsecase implements IUseCaseService<string> {
private readonly documentContractFacade: DocumentContractFacade,
private readonly exceptionService: ExceptionsService,
private readonly actionsArchiveFacade: ActionsArchiveFacade,
private readonly signatureService: DocumentSignatureFacade,
private readonly s3Service: S3Service,
) {}

public async execute(id: string, admin: IAdminUserModel): Promise<void> {
Expand Down Expand Up @@ -53,6 +58,34 @@ export class DeleteDocumentContractUsecase implements IUseCaseService<string> {
throw new Error('Could not delete contract from DB');
}

// Delete file from S3
try {
if (contract.filePath) {
await this.s3Service.deleteFile(contract.filePath);
}
} catch (error) {
Sentry.captureException(error);
this.logger.error('Error deleting contract file from S3: ', error);
}

// Delete signatures
try {
if (contract.volunteerSignatureId) {
await this.signatureService.delete(contract.volunteerSignatureId);
}
if (contract.legalGuardianSignatureId) {
await this.signatureService.delete(contract.legalGuardianSignatureId);
}
if (contract.ngoLegalRepresentativeSignatureId) {
await this.signatureService.delete(
contract.ngoLegalRepresentativeSignatureId,
);
}
} catch (error) {
Sentry.captureException(error);
this.logger.error('Error while deleting contract signatures');
}

// 10. Track event
this.actionsArchiveFacade.trackEvent(
TrackedEventName.DELETE_DOCUMENT_CONTRACT,
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/DocumentContractsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import ConfirmationModal from './ConfirmationModal';
import { useErrorToast, useSuccessToast } from '../hooks/useToast';
import { InternalErrors } from '../common/errors/internal-errors.class';
import { ContractsStatistics } from './ContractsStatistics';
import { useQueryClient } from 'react-query';

interface StatusOption {
key: string;
Expand Down Expand Up @@ -122,6 +123,8 @@ export interface DocumentContractsTableQueryProps extends IPaginationQueryParams
type DocumentContractsTableBasicProps = IHOCQueryProps<DocumentContractsTableQueryProps>;

const DocumentContractsTable = ({ query, setQuery }: DocumentContractsTableBasicProps) => {
const queryClient = useQueryClient();

// selected contract id
const [selectedContract, setSelectedContract] = useState<string>();
const [selectedVolunteer, setSelectedVolunteer] = useState<ListItem>();
Expand Down Expand Up @@ -187,6 +190,7 @@ const DocumentContractsTable = ({ query, setQuery }: DocumentContractsTableBasic
onSuccess: () => {
useSuccessToast(t('contract.submit.delete'));
setSelectedDeleteContract(null);
queryClient.invalidateQueries({ queryKey: ['contracts-statistics'] });
refetch();
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit da540ed

Please sign in to comment.