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 2 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, 'handleLocationVote');

await proposalContractingController.revertLocationVote(params, input, request);
expect(proposalContractingService.handleLocationVote).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 @@ -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)
@Put(':id/revertLocationVote')
af-jml marked this conversation as resolved.
Show resolved Hide resolved
@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.handleLocationVote(id, location, user);
}

@Auth(Role.FdpgMember)
@Put(':id/init-contracting')
@UsePipes(ValidationPipe)
Expand Down
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 */
FdpgRevertedLocationVote = 'FDPG_REVERTED_LOCATION_VOTE',
af-jml marked this conversation as resolved.
Show resolved Hide resolved

/** 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 @@ -137,6 +146,7 @@ describe('ProposalContractingService', () => {
handleProposalUacApproval: jest.fn(),
handleProposalStatusChange: jest.fn(),
handleProposalContractSign: jest.fn(),
handleLocationVote: jest.fn(),
af-jml marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
Expand All @@ -151,6 +161,12 @@ describe('ProposalContractingService', () => {
handleEffects: jest.fn(),
},
},
{
provide: ProposalUploadService,
useValue: {
handleEffects: jest.fn(),
},
},
],
imports: [],
}).compile();
Expand All @@ -160,6 +176,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 +245,29 @@ describe('ProposalContractingService', () => {
});
});

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

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

expect(validateRevertLocationVote).toHaveBeenCalledWith(proposalDocument);
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 handleLocationVote(proposalId: string, location: MiiLocation, user: IRequestUser): Promise<void> {
af-jml marked this conversation as resolved.
Show resolved Hide resolved
const toBeUpdated = await this.proposalCrudService.findDocument(proposalId, user, undefined, true);
validateRevertLocationVote(toBeUpdated);

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

await toBeUpdated.save();
}

async initContracting(
proposalId: string,
file: Express.Multer.File,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { clearLocationsVotes } from '../location-flow.util';

jest.mock('../location-flow.util', () => ({
clearLocationsVotes: jest.fn(),
deleteConditionalUpload: jest.fn(),
af-jml marked this conversation as resolved.
Show resolved Hide resolved
}));

jest.mock('../add-fdpg-task.util', () => ({
Expand Down
15 changes: 15 additions & 0 deletions src/modules/proposal/utils/__tests__/proposal-history.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
addHistoryItemForContractSystemReject,
addHistoryItemForProposalLock,
addHistoryItemForUnselectedLocation,
addHistoryItemForRevertLocationVote,
} from '../proposal-history.util';

const proposalId = 'proposalId';
Expand Down Expand Up @@ -191,6 +192,20 @@ describe('ProposalHistoryUtil', () => {
});
});

describe('addHistoryItemForRevertLocationVote', () => {
it('should add history item for reverted location vote', () => {
const request = getRequest();
const proposal = getProposalDocument();
const location = MiiLocation.UKRUB;
af-jml marked this conversation as resolved.
Show resolved Hide resolved

addHistoryItemForRevertLocationVote(proposal, request.user, location);
expect(proposal.history.length).toBe(1);

const expectedType = HistoryEventType.FdpgRevertedLocationVote;
expect(proposal.history[0].type).toBe(expectedType);
});
});

describe('addHistoryItemForUacCondition', () => {
test.each([true, false])(`should add history item for uac condition (isApproved: %s)`, (isApproved) => {
const request = getRequest();
Expand Down
Loading
Loading