Skip to content

Commit

Permalink
[ZARS-685][DELETE] rollback changes for s3 proposal name
Browse files Browse the repository at this point in the history
  • Loading branch information
af-egr committed Dec 13, 2024
1 parent 8832158 commit bb8eded
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { IGetKeycloakUser } from 'src/modules/user/types/keycloak-user.interface
import { addHistoryItemForProposalLock, addHistoryItemForStatus } from '../../utils/proposal-history.util';
import { validateStatusChange } from '../../utils/validate-status-change.util';
import { setImmediate } from 'timers';
import { addUpload, getBlobName, getProposalBlobName } from '../../utils/proposal.utils';
import { addUpload, getBlobName } from '../../utils/proposal.utils';
import { UseCaseUpload } from '../../enums/upload-type.enum';
import { SupportedMimetype } from '../../enums/supported-mime-type.enum';
import { FdpgChecklistSetDto } from '../../dto/proposal/fdpg-checklist.dto';
Expand Down Expand Up @@ -50,7 +50,6 @@ jest.mock('../../utils/proposal-history.util', () => ({
jest.mock('../../utils/proposal.utils', () => ({
addUpload: jest.fn(),
getBlobName: jest.fn().mockReturnValue('blobName'),
getProposalBlobName: jest.fn().mockReturnValue('blobName_proposal'),
}));

jest.mock('../../utils/validate-fdpg-check-status.util', () => ({
Expand Down Expand Up @@ -363,8 +362,7 @@ describe('ProposalMiscService', () => {
originalname: `${proposalDocument.projectAbbreviation}_proposal.pdf`,
mimetype: SupportedMimetype.Pdf,
});
expect(storageService.uploadFile).toHaveBeenCalledWith('blobName_proposal', expectedFile, request.user);
expect(getProposalBlobName).toHaveBeenCalledWith(proposalId, projectAbbreviation, UseCaseUpload.ProposalPDF);
expect(storageService.uploadFile).toHaveBeenCalledWith('blobName', expectedFile, request.user);
expect(addUpload).toHaveBeenCalledWith(proposalDocument, expect.anything());
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/modules/proposal/services/proposal-misc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { UseCaseUpload } from '../enums/upload-type.enum';
import { addFdpgChecklist } from '../utils/add-fdpg-checklist.util';
import { flattenToLanguage } from '../utils/flatten-to-language.util';
import { addHistoryItemForProposalLock, addHistoryItemForStatus } from '../utils/proposal-history.util';
import { addUpload, getBlobName, getProposalBlobName } from '../utils/proposal.utils';
import { addUpload, getBlobName } from '../utils/proposal.utils';
import { validateFdpgCheckStatus } from '../utils/validate-fdpg-check-status.util';
import { validateStatusChange } from '../utils/validate-status-change.util';
import { ProposalCrudService } from './proposal-crud.service';
Expand Down Expand Up @@ -148,7 +148,7 @@ export class ProposalMiscService {
mimetype: SupportedMimetype.Pdf,
size: Buffer.byteLength(pdfBuffer),
} as Express.Multer.File;
const pdfBlobName = getProposalBlobName(proposal._id, proposal.projectAbbreviation, UseCaseUpload.ProposalPDF);
const pdfBlobName = getBlobName(proposal._id, UseCaseUpload.ProposalPDF);
await this.storageService.uploadFile(pdfBlobName, pdfFile, user);
const pdfUpload = new UploadDto(pdfBlobName, pdfFile, UseCaseUpload.ProposalPDF, user);
addUpload(proposal, pdfUpload);
Expand Down
22 changes: 5 additions & 17 deletions src/modules/proposal/utils/proposal.utils.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import { UploadType, UseCaseUpload } from '../enums/upload-type.enum';
import { UploadType } from '../enums/upload-type.enum';
import { v4 as uuid } from 'uuid';
import { Proposal } from '../schema/proposal.schema';
import { UploadDto } from '../dto/upload.dto';
import { ReportDto } from '../dto/proposal/report.dto';

const getBasePath = (proposalId: string, type: UploadType): string => `proposal/${proposalId}/${type}/`;

export const getBlobName = (proposalId: string, type: UploadType, subId?: string): string => {
return `${getBasePath(proposalId, type)}${getSubPath(subId)}${uuid()}`;
};

export const getProposalBlobName = (proposalId: string, projectAbbreviation: string, type: UploadType) => {
if (type === UseCaseUpload.ProposalPDF) {
const blobInstanceName = `${projectAbbreviation}_proposal`;

return `${getBasePath(proposalId, type)}${blobInstanceName}`;
}

throw new Error(
`type '${type}' of proposalId '${proposalId}' does not match requirement '${UseCaseUpload.ProposalPDF}'`,
);
};

const getSubPath = (subId?: string): string => {
return subId ? subId + '/' : '';
};

export const getBlobName = (proposalId: string, type: UploadType, subId?: string): string => {
return `${getBasePath(proposalId, type)}${getSubPath(subId)}${uuid()}`;
};

export const addUpload = (proposal: Proposal, upload: UploadDto): void => {
if (proposal.uploads) {
proposal.uploads.push(upload);
Expand Down

0 comments on commit bb8eded

Please sign in to comment.