Skip to content

Commit

Permalink
[ZARS-651][FIX] projection fix
Browse files Browse the repository at this point in the history
  • Loading branch information
af-ak committed Jan 8, 2024
1 parent 8671dc8 commit 0151320
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/modules/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class CommentService {
const proposal = await this.proposalCrudService.findDocument(
commentReference.referenceDocumentId,
user,
undefined,
projection,
false,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('ProposalCrudService', () => {
const desiredProjection = { ['reports.content']: 1 };
const willBeModified = true;

const proposal = { _id: proposalId, owner: null } as any as ProposalDocument;
const proposal = { _id: proposalId, owner: { name: 'Lars' } } as any as ProposalDocument;
const proposalWithOwner = { ...proposal, owner: { name: 'Lars' } } as any as ProposalDocument;
ProposalModel.findById.mockResolvedValueOnce(proposalWithOwner);

Expand All @@ -243,6 +243,57 @@ describe('ProposalCrudService', () => {
expect(validateProposalAccess).toBeCalledWith(proposal, request.user, willBeModified);
});

it('should find a proposal with diz member projection', async () => {
const proposalId = 'proposalId';
const user = { ...request.user, singleKnownRole: Role.DizMember };
const expectedProjection = {
['reports.content']: 1,
owner: 1,
conditionalApprovals: 1,
openDizChecks: 1,
dizApprovedLocations: 1,
uacApprovedLocations: 1,
signedContracts: 1,
requestedButExcludedLocations: 1,
};
const desiredProjection = { ['reports.content']: 1 };
const willBeModified = true;

const proposal = { _id: proposalId, owner: { name: 'Lars' } } as any as ProposalDocument;
const proposalWithOwner = { ...proposal, owner: { name: 'Lars' } } as any as ProposalDocument;
ProposalModel.findById.mockResolvedValueOnce(proposalWithOwner);

const result = await proposalCrudService.findDocument(proposalId, user, desiredProjection, willBeModified);
expect(result).toEqual(proposal);
expect(ProposalModel.findById).toBeCalledWith(proposalId, expectedProjection);
expect(validateProposalAccess).toBeCalledWith(proposal, user, willBeModified);
});

it('should find a proposal with uac member projection', async () => {
const proposalId = 'proposalId';
const user = { ...request.user, singleKnownRole: Role.UacMember };
const expectedProjection = {
['reports.content']: 1,
owner: 1,
conditionalApprovals: 1,
dizApprovedLocations: 1,
uacApprovedLocations: 1,
signedContracts: 1,
requestedButExcludedLocations: 1,
};
const desiredProjection = { ['reports.content']: 1 };
const willBeModified = true;

const proposal = { _id: proposalId, owner: { name: 'Lars' } } as any as ProposalDocument;
const proposalWithOwner = { ...proposal, owner: { name: 'Lars' } } as any as ProposalDocument;
ProposalModel.findById.mockResolvedValueOnce(proposalWithOwner);

const result = await proposalCrudService.findDocument(proposalId, user, desiredProjection, willBeModified);
expect(result).toEqual(proposal);
expect(ProposalModel.findById).toBeCalledWith(proposalId, expectedProjection);
expect(validateProposalAccess).toBeCalledWith(proposal, user, willBeModified);
});

it('should throw 404 if not found', async () => {
const proposalId = 'proposalId';
const desiredProjection = undefined;
Expand Down
21 changes: 16 additions & 5 deletions src/modules/proposal/services/proposal-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { addHistoryItemForStatus } from '../utils/proposal-history.util';
import { validateProposalAccess } from '../utils/validate-access.util';
import { validateStatusChange } from '../utils/validate-status-change.util';
import { CheckUniqueProposalDto } from '../dto/check-unique-proposal.dto';
import { Role } from 'src/shared/enums/role.enum';

@Injectable()
export class ProposalCrudService {
Expand Down Expand Up @@ -65,18 +66,28 @@ export class ProposalCrudService {

if (projection === undefined) {
dbProjection['reports.content'] = 0;
} else if (user.singleKnownRole === Role.DizMember) {
dbProjection.owner = 1;
dbProjection.conditionalApprovals = 1;
dbProjection.openDizChecks = 1;
dbProjection.dizApprovedLocations = 1;
dbProjection.uacApprovedLocations = 1;
dbProjection.signedContracts = 1;
dbProjection.requestedButExcludedLocations = 1;
} else if (user.singleKnownRole === Role.UacMember) {
dbProjection.owner = 1;
dbProjection.conditionalApprovals = 1;
dbProjection.dizApprovedLocations = 1;
dbProjection.uacApprovedLocations = 1;
dbProjection.signedContracts = 1;
dbProjection.requestedButExcludedLocations = 1;
} else {
dbProjection.owner = 1;
}
const proposal = await this.proposalModel.findById(proposalId, dbProjection);

if (proposal) {
validateProposalAccess(proposal, user, willBeModified);
// owner must be available to check access
if (projection && projection.owner !== 1) {
// This reset + a new save would cause the owner to be overwritten with null!
proposal.owner = null;
}
return proposal;
} else {
throw new NotFoundException();
Expand Down

0 comments on commit 0151320

Please sign in to comment.