Skip to content

Commit

Permalink
[ADD] expose location for history
Browse files Browse the repository at this point in the history
  • Loading branch information
af-ofr committed May 7, 2024
1 parent a867fd5 commit a525bf4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
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.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);
};
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;
}
19 changes: 12 additions & 7 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 @@ -58,6 +58,17 @@ export class ProposalUploadService {

async deleteUpload(proposalId: string, uploadId: string, user: IRequestUser): Promise<void> {
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<void> {
const uploadIndex = proposal.uploads.findIndex((upload) => upload._id.toString() === uploadId);

if (uploadIndex === -1) {
Expand All @@ -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);
}
}
}
19 changes: 8 additions & 11 deletions src/modules/proposal/utils/revert-location-vote.util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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';
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,
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a525bf4

Please sign in to comment.