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

ZARS-666 revert location vote #24

Merged
merged 16 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ services:
- 8081:3000
environment:
ENABLE_TELEMETRY: false
PRINT_FRONTEND_HOST: http://localhost:3000
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Role } from 'src/shared/enums/role.enum';
import { FdpgRequest } from 'src/shared/types/request-user.interface';
import { SetDizApprovalDto } from '../../dto/set-diz-approval.dto';
import { SetUacApprovalDto } from '../../dto/set-uac-approval.dto';
import { RevertLocationVoteDto } from '../../dto/revert-location-vote.dto';
import { SignContractDto } from '../../dto/sign-contract.dto';
import { ProposalContractingController } from '../proposal-contracting.controller';
import { ProposalContractingService } from '../../services/proposal-contracting.service';
Expand Down Expand Up @@ -75,6 +76,23 @@ describe('ProposalContractingController', () => {
});
});

describe('revertLocationVote', () => {
it('should revert the location vote', async () => {
const params = {
id: 'mongoId',
};
const input = new RevertLocationVoteDto();
jest.spyOn(proposalContractingService, 'revertLocationVote');

await proposalContractingController.revertLocationVote(params, input, request);
expect(proposalContractingService.revertLocationVote).toHaveBeenCalledWith(
params.id,
input.location,
request.user,
);
});
});

describe('signContract', () => {
it('should set the contract sign', async () => {
const params = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ describe('ProposalUploadController', () => {
jest.spyOn(proposalUploadService, 'deleteUpload');

await proposalUploadController.deleteUpload(params, request);
expect(proposalUploadService.deleteUpload).toHaveBeenCalledWith(params.mainId, params.subId, request.user);
expect(proposalUploadService.deleteUploadAndSaveProposal).toHaveBeenCalledWith(
params.mainId,
params.subId,
request.user,
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import { ContractingUploadDto } from '../dto/contracting-upload.dto';
import { ProposalMarkConditionAcceptedReturnDto } from '../dto/proposal/proposal.dto';
import { SetDizApprovalDto } from '../dto/set-diz-approval.dto';
import { SetUacApprovalDto, SetUacApprovalWithFileDto } from '../dto/set-uac-approval.dto';
import { RevertLocationVoteDto } from '../dto/revert-location-vote.dto';
import { SignContractDto, SignContractWithFileDto } from '../dto/sign-contract.dto';
import { ProposalContractingService } from '../services/proposal-contracting.service';
import { InitContractingDto } from '../dto/proposal/init-contracting.dto';
import { ProposalContractingService } from '../services/proposal-contracting.service';

@ApiController('proposals', undefined, 'contracting')
export class ProposalContractingController {
Expand Down Expand Up @@ -66,6 +67,22 @@ export class ProposalContractingController {
return await this.proposalContractingService.setUacApproval(id, vote, file, user);
}

@Auth(Role.FdpgMember)
@Post(':id/revertLocationVote')
@UsePipes(ValidationPipe)
@ApiNotFoundResponse({ description: 'Proposal could not be found' })
@ApiOperation({ summary: 'FDPG member reverts locations vote ' })
@ApiNoContentResponse({ description: 'Locations vote reverted. No content returns.' })
@HttpCode(204)
@ApiBody({ type: RevertLocationVoteDto })
async revertLocationVote(
@Param() { id }: MongoIdParamDto,
@Body() { location }: RevertLocationVoteDto,
@Request() { user }: FdpgRequest,
): Promise<void> {
return await this.proposalContractingService.revertLocationVote(id, location, user);
}

@Auth(Role.FdpgMember)
@Put(':id/init-contracting')
@UsePipes(ValidationPipe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ export class ProposalUploadController {
@HttpCode(204)
@ApiOperation({ summary: 'Deletes a proposal upload' })
async deleteUpload(@Param() { mainId, subId }: MongoTwoIdsParamDto, @Request() { user }: FdpgRequest): Promise<void> {
return await this.proposalUploadService.deleteUpload(mainId, subId, user);
return await this.proposalUploadService.deleteUploadAndSaveProposal(mainId, subId, user);
}
}
18 changes: 15 additions & 3 deletions src/modules/proposal/decorators/expose-history.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { RevertLocationVoteDto } from './../dto/revert-location-vote.dto';
import { Expose, plainToClass, Transform } from 'class-transformer';
import { parseGroupToUser } from 'src/shared/utils/user-group.utils';
import { HistoryEventGetDto } from '../dto/proposal/history-event.dto';
import { HistoryEvent } from '../schema/sub-schema/history-event.schema';
import { HistoryEventType } from '../enums/history-event.enum';

export const ExposeHistory = () => (target: Object, propertyKey: string) => {
Expose()(target, propertyKey);
Transform((params) => {
const user = parseGroupToUser(params.options.groups);
const filteredEvents = params.obj[propertyKey].filter((event: HistoryEvent) =>
event.location ? event.location === user.miiLocation : true,
const isFdpgMember = user.singleKnownRole === 'FdpgMember';

const filteredEvents = params.obj[propertyKey].filter((event: HistoryEvent) => {
const isRevertEvent = event.type === HistoryEventType.FdpgLocationVoteReverted;

if (isRevertEvent && isFdpgMember) {
return true;
}
return event.location ? event.location === user.miiLocation : true;
});

return filteredEvents.map((history) =>
plainToClass(HistoryEventGetDto, history, { groups: params.options.groups }),
);
return filteredEvents.map((history) => plainToClass(HistoryEventGetDto, history));
})(target, propertyKey);
};
5 changes: 5 additions & 0 deletions src/modules/proposal/dto/proposal/history-event.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { MiiLocation } from 'src/shared/constants/mii-locations';
import { Exclude, Expose } from 'class-transformer';
import { Version } from 'src/shared/schema/version.schema';
import { HistoryEventType } from '../../enums/history-event.enum';
import { Role } from 'src/shared/enums/role.enum';

@Exclude()
export class HistoryEventGetDto {
Expand All @@ -12,4 +14,7 @@ export class HistoryEventGetDto {

@Expose()
createdAt: Date;

@Expose()
location: MiiLocation;
}
10 changes: 10 additions & 0 deletions src/modules/proposal/dto/revert-location-vote.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Exclude, Expose } from 'class-transformer';
import { IsEnum } from 'class-validator';
import { MiiLocation } from 'src/shared/constants/mii-locations';

@Exclude()
export class RevertLocationVoteDto {
@Expose()
@IsEnum(MiiLocation)
location: MiiLocation;
}
3 changes: 3 additions & 0 deletions src/modules/proposal/enums/history-event.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export enum HistoryEventType {
/** FDPG Veto */
FdpgApprovedLocationRemoved = 'FDPG_APPROVED_LOCATION_REMOVED',

/** FDPG reverts DIZ and UAC vote */
FdpgLocationVoteReverted = 'FDPG_LOCATION_VOTE_REVERTED',

/** Data Delivery */

/** Contracting */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ForbiddenException, NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { StorageService } from 'src/modules/storage/storage.service';
import { EventEngineService } from 'src/modules/event-engine/event-engine.service';
import { StorageService } from 'src/modules/storage/storage.service';
import { MiiLocation } from 'src/shared/constants/mii-locations';
import { Role } from 'src/shared/enums/role.enum';
import { FdpgRequest } from 'src/shared/types/request-user.interface';
import { NoErrorThrownError, getError } from 'test/get-error';
import { InitContractingDto } from '../../dto/proposal/init-contracting.dto';
import { SetDizApprovalDto } from '../../dto/set-diz-approval.dto';
import { SetUacApprovalDto } from '../../dto/set-uac-approval.dto';
import { SignContractDto } from '../../dto/sign-contract.dto';
Expand All @@ -14,19 +17,19 @@ import { addDizApproval, addUacApproval, addUacApprovalWithCondition } from '../
import {
addHistoryItemForContractSign,
addHistoryItemForDizApproval,
addHistoryItemForRevertLocationVote,
addHistoryItemForStatus,
addHistoryItemForUacApproval,
} from '../../utils/proposal-history.util';
import { addUpload } from '../../utils/proposal.utils';
import { revertLocationVote } from '../../utils/revert-location-vote.util';
import { validateContractSign } from '../../utils/validate-contract-sign.util';
import { validateStatusChange } from '../../utils/validate-status-change.util';
import { validateDizApproval, validateUacApproval } from '../../utils/validate-vote.util';
import { validateDizApproval, validateRevertLocationVote, validateUacApproval } from '../../utils/validate-vote.util';
import { ProposalContractingService } from '../proposal-contracting.service';
import { ProposalCrudService } from '../proposal-crud.service';
import { ProposalUploadService } from '../proposal-upload.service';
import { StatusChangeService } from '../status-change.service';
import { ForbiddenException, NotFoundException } from '@nestjs/common';
import { NoErrorThrownError, getError } from 'test/get-error';
import { InitContractingDto } from '../../dto/proposal/init-contracting.dto';

jest.mock('class-transformer', () => {
const original = jest.requireActual('class-transformer');
Expand All @@ -39,6 +42,7 @@ jest.mock('class-transformer', () => {
jest.mock('../../utils/validate-vote.util', () => ({
validateDizApproval: jest.fn(),
validateUacApproval: jest.fn(),
validateRevertLocationVote: jest.fn(),
}));

jest.mock('../../utils/add-location-vote.util', () => ({
Expand All @@ -47,13 +51,17 @@ jest.mock('../../utils/add-location-vote.util', () => ({
addUacApprovalWithCondition: jest.fn(),
addUacConditionReview: jest.fn(),
}));
jest.mock('../../utils/revert-location-vote.util', () => ({
revertLocationVote: jest.fn(),
}));

jest.mock('../../utils/proposal-history.util', () => ({
addHistoryItemForContractSign: jest.fn(),
addHistoryItemForDizApproval: jest.fn(),
addHistoryItemForStatus: jest.fn(),
addHistoryItemForUacApproval: jest.fn(),
addHistoryItemForUacCondition: jest.fn(),
addHistoryItemForRevertLocationVote: jest.fn(),
}));

jest.mock('../../utils/proposal.utils', () => ({
Expand Down Expand Up @@ -85,6 +93,7 @@ describe('ProposalContractingService', () => {
let eventEngineService: jest.Mocked<EventEngineService>;
let storageService: jest.Mocked<StorageService>;
let statusChangeService: jest.Mocked<StatusChangeService>;
let proposalUploadService: jest.Mocked<ProposalUploadService>;

const request = {
user: {
Expand Down Expand Up @@ -151,6 +160,12 @@ describe('ProposalContractingService', () => {
handleEffects: jest.fn(),
},
},
{
provide: ProposalUploadService,
useValue: {
handleEffects: jest.fn(),
},
},
],
imports: [],
}).compile();
Expand All @@ -160,6 +175,9 @@ describe('ProposalContractingService', () => {
eventEngineService = module.get<EventEngineService>(EventEngineService) as jest.Mocked<EventEngineService>;
storageService = module.get<StorageService>(StorageService) as jest.Mocked<StorageService>;
statusChangeService = module.get<StatusChangeService>(StatusChangeService) as jest.Mocked<StatusChangeService>;
proposalUploadService = module.get<ProposalUploadService>(
ProposalUploadService,
) as jest.Mocked<ProposalUploadService>;
});

it('should be defined', () => {
Expand Down Expand Up @@ -226,6 +244,30 @@ describe('ProposalContractingService', () => {
});
});

describe('revertLocationVote', () => {
it('should revert the location vote', async () => {
const proposalDocument = getProposalDocument();
const location = MiiLocation.UKL;
jest.spyOn(proposalCrudService, 'findDocument').mockResolvedValueOnce(proposalDocument);

await proposalContractingService.revertLocationVote(proposalId, request.user.miiLocation, request.user);

expect(validateRevertLocationVote).toHaveBeenCalledWith(proposalDocument, location);
expect(revertLocationVote).toHaveBeenCalledWith(
proposalDocument,
request.user.miiLocation,
request.user,
proposalUploadService,
);
expect(addHistoryItemForRevertLocationVote).toHaveBeenCalledWith(
proposalDocument,
request.user,
request.user.miiLocation,
);
expect(proposalDocument.save).toHaveBeenCalledTimes(1);
});
});

describe('initContracting', () => {
test.each([true, false])('should init the contracting', async (isValidStatus: boolean) => {
const proposalStatus = isValidStatus ? ProposalStatus.LocationCheck : ProposalStatus.Draft;
Expand Down
34 changes: 24 additions & 10 deletions src/modules/proposal/services/proposal-contracting.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
import { plainToClass } from 'class-transformer';
import { isNotEmptyObject } from 'class-validator';
import { ValidationException } from 'src/exceptions/validation/validation.exception';
import { MiiLocation } from 'src/shared/constants/mii-locations';
import { ValidationErrorInfo } from 'src/shared/dto/validation/validation-error-info.dto';
import { BadRequestError } from 'src/shared/enums/bad-request-error.enum';
import { Role } from 'src/shared/enums/role.enum';
import { IRequestUser } from 'src/shared/types/request-user.interface';
import { convertUserToGroups } from 'src/shared/utils/user-group.utils';
import { StorageService } from '../../storage/storage.service';
import { EventEngineService } from '../../event-engine/event-engine.service';
import { StorageService } from '../../storage/storage.service';
import { InitContractingDto } from '../dto/proposal/init-contracting.dto';
import { ProposalMarkConditionAcceptedReturnDto } from '../dto/proposal/proposal.dto';
import { SetDizApprovalDto } from '../dto/set-diz-approval.dto';
import { SetUacApprovalDto } from '../dto/set-uac-approval.dto';
import { SignContractDto } from '../dto/sign-contract.dto';
import { UploadDto } from '../dto/upload.dto';
import { ProposalValidation } from '../enums/porposal-validation.enum';
import { ProposalStatus } from '../enums/proposal-status.enum';
import { UseCaseUpload } from '../enums/upload-type.enum';
import { ProposalCrudService } from './proposal-crud.service';
import { StatusChangeService } from './status-change.service';
import { addContractSign } from '../utils/add-contract-sign.util';
import {
addDizApproval,
Expand All @@ -25,19 +28,19 @@ import {
import {
addHistoryItemForContractSign,
addHistoryItemForDizApproval,
addHistoryItemForRevertLocationVote,
addHistoryItemForStatus,
addHistoryItemForUacApproval,
addHistoryItemForUacCondition,
} from '../utils/proposal-history.util';
import { addUpload, getBlobName } from '../utils/proposal.utils';
import { revertLocationVote } from '../utils/revert-location-vote.util';
import { validateContractSign } from '../utils/validate-contract-sign.util';
import { validateStatusChange } from '../utils/validate-status-change.util';
import { validateDizApproval, validateUacApproval } from '../utils/validate-vote.util';
import { SetDizApprovalDto } from '../dto/set-diz-approval.dto';
import { InitContractingDto } from '../dto/proposal/init-contracting.dto';
import { ValidationErrorInfo } from 'src/shared/dto/validation/validation-error-info.dto';
import { ValidationException } from 'src/exceptions/validation/validation.exception';
import { BadRequestError } from 'src/shared/enums/bad-request-error.enum';
import { validateDizApproval, validateRevertLocationVote, validateUacApproval } from '../utils/validate-vote.util';
import { ProposalCrudService } from './proposal-crud.service';
import { ProposalUploadService } from './proposal-upload.service';
import { StatusChangeService } from './status-change.service';
af-jml marked this conversation as resolved.
Show resolved Hide resolved

@Injectable()
export class ProposalContractingService {
Expand All @@ -46,6 +49,7 @@ export class ProposalContractingService {
private eventEngineService: EventEngineService,
private storageService: StorageService,
private statusChangeService: StatusChangeService,
private proposalUploadService: ProposalUploadService,
) {}

async setDizApproval(proposalId: string, vote: SetDizApprovalDto, user: IRequestUser): Promise<void> {
Expand Down Expand Up @@ -86,6 +90,16 @@ export class ProposalContractingService {
await this.eventEngineService.handleProposalUacApproval(saveResult, vote.value, user.miiLocation);
}

async revertLocationVote(proposalId: string, location: MiiLocation, user: IRequestUser): Promise<void> {
const toBeUpdated = await this.proposalCrudService.findDocument(proposalId, user, undefined, true);
validateRevertLocationVote(toBeUpdated, location);

await revertLocationVote(toBeUpdated, location, user, this.proposalUploadService);
addHistoryItemForRevertLocationVote(toBeUpdated, user, location);

await toBeUpdated.save();
}

async initContracting(
proposalId: string,
file: Express.Multer.File,
Expand Down
11 changes: 8 additions & 3 deletions src/modules/proposal/services/proposal-upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StorageService } from '../../storage/storage.service';
import { UploadDto, UploadGetDto } from '../dto/upload.dto';
import { DirectUpload } from '../enums/upload-type.enum';
import { ProposalCrudService } from './proposal-crud.service';
import { Proposal } from '../schema/proposal.schema';
import { Proposal, ProposalDocument } from '../schema/proposal.schema';
import { addUpload, getBlobName } from '../utils/proposal.utils';
import { validateUploadDeletion } from '../utils/validate-upload-deletion.util';

Expand Down Expand Up @@ -56,8 +56,7 @@ export class ProposalUploadService {
}
}

async deleteUpload(proposalId: string, uploadId: string, user: IRequestUser): Promise<void> {
const proposal = await this.proposalCrudService.findDocument(proposalId, user, undefined, true);
async deleteUpload(proposal: ProposalDocument, uploadId: string, user: IRequestUser): Promise<void> {
const uploadIndex = proposal.uploads.findIndex((upload) => upload._id.toString() === uploadId);

if (uploadIndex === -1) {
Expand All @@ -71,6 +70,12 @@ export class ProposalUploadService {
await this.storageService.deleteBlob(upload.blobName);

proposal.uploads.splice(uploadIndex, 1);
}

async deleteUploadAndSaveProposal(proposalId: string, uploadId: string, user: IRequestUser): Promise<void> {
const proposal = await this.proposalCrudService.findDocument(proposalId, user, undefined, true);

await this.deleteUpload(proposal, uploadId, user);

try {
await proposal.save();
Expand Down
Loading
Loading