From a525bf46874eb9523570fa8278ad209fea6e1a41 Mon Sep 17 00:00:00 2001 From: Aleksandra Frost Date: Tue, 7 May 2024 15:30:05 +0200 Subject: [PATCH] [ADD] expose location for history --- .../decorators/expose-history.decorator.ts | 18 +++++++++++++++--- .../dto/proposal/history-event.dto.ts | 5 +++++ .../services/proposal-upload.service.ts | 19 ++++++++++++------- .../utils/revert-location-vote.util.ts | 19 ++++++++----------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/modules/proposal/decorators/expose-history.decorator.ts b/src/modules/proposal/decorators/expose-history.decorator.ts index e1ccb0e..8a56bd3 100644 --- a/src/modules/proposal/decorators/expose-history.decorator.ts +++ b/src/modules/proposal/decorators/expose-history.decorator.ts @@ -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.FdpgRevertedLocationVote; + + 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); }; diff --git a/src/modules/proposal/dto/proposal/history-event.dto.ts b/src/modules/proposal/dto/proposal/history-event.dto.ts index 9202dac..957c866 100644 --- a/src/modules/proposal/dto/proposal/history-event.dto.ts +++ b/src/modules/proposal/dto/proposal/history-event.dto.ts @@ -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 { @@ -12,4 +14,7 @@ export class HistoryEventGetDto { @Expose() createdAt: Date; + + @Expose() + location: MiiLocation; } diff --git a/src/modules/proposal/services/proposal-upload.service.ts b/src/modules/proposal/services/proposal-upload.service.ts index 9f4b6b0..543484b 100644 --- a/src/modules/proposal/services/proposal-upload.service.ts +++ b/src/modules/proposal/services/proposal-upload.service.ts @@ -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'; @@ -58,6 +58,17 @@ export class ProposalUploadService { async deleteUpload(proposalId: string, uploadId: string, user: IRequestUser): Promise { const proposal = await this.proposalCrudService.findDocument(proposalId, user, undefined, true); + + await this.delete2Upload(proposal, uploadId, user); + + try { + await proposal.save(); + } catch (error) { + throw new Error(error); + } + } + + async delete2Upload(proposal: ProposalDocument, uploadId: string, user: IRequestUser): Promise { const uploadIndex = proposal.uploads.findIndex((upload) => upload._id.toString() === uploadId); if (uploadIndex === -1) { @@ -71,11 +82,5 @@ export class ProposalUploadService { await this.storageService.deleteBlob(upload.blobName); proposal.uploads.splice(uploadIndex, 1); - - try { - await proposal.save(); - } catch (error) { - throw new Error(error); - } } } diff --git a/src/modules/proposal/utils/revert-location-vote.util.ts b/src/modules/proposal/utils/revert-location-vote.util.ts index cddf432..94ad059 100644 --- a/src/modules/proposal/utils/revert-location-vote.util.ts +++ b/src/modules/proposal/utils/revert-location-vote.util.ts @@ -1,6 +1,6 @@ import { getLocationState } from './validate-access.util'; import { ProposalUploadService } from '../services/proposal-upload.service'; -import { Proposal } from '../schema/proposal.schema'; +import { Proposal, ProposalDocument } from '../schema/proposal.schema'; import { MiiLocation } from 'src/shared/constants/mii-locations'; import { IRequestUser } from 'src/shared/types/request-user.interface'; import { removeFdpgTask } from './add-fdpg-task.util'; @@ -8,7 +8,7 @@ import { FdpgTaskType } from '../enums/fdpg-task-type.enum'; import { clearLocationsVotes } from './location-flow.util'; export const revertLocationVote = async ( - proposal: Proposal, + proposal: ProposalDocument, location: MiiLocation, user: IRequestUser, proposalUploadService: ProposalUploadService, @@ -17,10 +17,10 @@ export const revertLocationVote = async ( let locationDataAmount = 0; - if (locationState.isConditionalApproval) { + if (locationState.conditionalApprovalAccepted) { locationDataAmount = proposal.conditionalApprovals.find((approval) => approval.location === location)?.dataAmount ?? 0; - } else { + } else if (!locationState.isConditionalApproval && locationState.uacApproved) { locationDataAmount = proposal.uacApprovals.find((approval) => approval.location === location)?.dataAmount ?? 0; } @@ -48,20 +48,17 @@ export const revertLocationVote = async ( const uploadId = proposal.conditionalApprovals.find((approval) => approval.location === location)?.uploadId; if (uploadId) { - await proposalUploadService.deleteUpload(proposal._id, uploadId, user); + await proposalUploadService.delete2Upload(proposal, uploadId, user); } - } - - if (locationState.conditionalApprovalAccepted) { removeFdpgTask(proposal, FdpgTaskType.ConditionApproval); proposal.conditionalApprovals = proposal.conditionalApprovals.filter( (condition) => condition.location !== location, ); + } - if (!isDataAmountReached) { - removeFdpgTask(proposal, FdpgTaskType.DataAmountReached); - } + if (locationState.conditionalApprovalAccepted && !isDataAmountReached) { + removeFdpgTask(proposal, FdpgTaskType.DataAmountReached); } clearLocationsVotes(proposal, location);