Skip to content

Commit

Permalink
feat: Optimisation: Passing user id as 0, if InstrumentPicker is not …
Browse files Browse the repository at this point in the history
…part of the answer, dont run the code.
  • Loading branch information
yoganandaness committed Jun 14, 2023
1 parent fd71d06 commit f276ba2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,52 @@ export default function createHandler() {
}

switch (event.type) {
case Event.TOPIC_ANSWERED: {
const {
questionarystep: { questionaryId },
} = event;
case Event.TOPIC_ANSWERED:
{
const {
questionarystep: { questionaryId },
} = event;

const instrumentPickerAnswer =
await questionaryDataSource.getLatestAnswerByQuestionaryIdAndDataType(
questionaryId,
DataType.INSTRUMENT_PICKER
);

const instrumentId = instrumentPickerAnswer?.answer.answer.value;
if (!instrumentId)
throw new Error(`Invalid Instrument id ${instrumentId}`);

const instrument = await instrumentDataSource.getInstrument(
instrumentId
);
const instrumentPickerAnswer =
await questionaryDataSource.getLatestAnswerByQuestionaryIdAndDataType(
questionaryId,
DataType.INSTRUMENT_PICKER
);

if (!instrument)
throw new Error(`Instrument with id ${instrumentId} not found`);
if (!instrumentPickerAnswer) break;
const instrumentId = instrumentPickerAnswer?.answer.answer.value;
if (!instrumentId)
throw new Error(`Invalid Instrument id ${instrumentId}`);

const proposal = await proposalDataSource.getByQuestionaryid(
questionaryId
);
if (!proposal)
throw new Error(
`Proposal with questionary id ${questionaryId} not found`
const instrument = await instrumentDataSource.getInstrument(
instrumentId
);
if (!instrument)
throw new Error(`Instrument with id ${instrumentId} not found`);
const proposal = await proposalDataSource.getByQuestionaryid(
questionaryId
);
if (!proposal)
throw new Error(
`Proposal with questionary id ${questionaryId} not found`
);
await instrumentMutations.assignProposalsToInstrumentHelper(
{ id: 0 } as UserWithRole,
{
proposals: [
{
primaryKey: proposal.primaryKey,
callId: proposal.callId,
},
],
instrumentId,
}
);
}

await instrumentDataSource.assignProposalsToInstrument(
[proposal.primaryKey],
instrumentId
);

await instrumentMutations.assignProposalsToInstrumentAfterEffect(
{} as UserWithRole,
{
proposals: [
{
primaryKey: proposal.primaryKey,
callId: proposal.callId,
},
],
instrumentId,
}
);
}
break;
default:
break;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const instrumentPickerDefinition: Question<DataType.INSTRUMENT_PICKER> =
transformConfig: async (config, helpers, callId) => {
const fallBackConfig = { ...config, instruments: [] };
try {
if (!callId) throw 'Call ID not available. ';
if (!callId) return fallBackConfig;
if (!helpers) throw 'Helpers not available. ';

const instruments = await helpers.fetchCallInstruments(callId);
Expand Down
48 changes: 19 additions & 29 deletions apps/user-office-backend/src/mutations/InstrumentMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,36 @@ export default class InstrumentMutations {
}

@EventBus(Event.PROPOSAL_INSTRUMENT_SELECTED)
async assignProposalsToInstrumentAfterEffect(
async assignProposalsToInstrumentHelper(
agent: UserWithRole | null,
args: AssignProposalsToInstrumentArgs
) {
const instrument = await this.dataSource.getInstrument(args.instrumentId);

if (!instrument) {
return rejection('Instrument Not found', { agent, args });
return rejection(
'Cannot assign the proposal to the instrument because the proposals call has no such instrument',
{ agent, args }
);
}

const proposalPks = args.proposals.map((proposal) => proposal.primaryKey);

const result = await this.dataSource.assignProposalsToInstrument(
proposalPks,
args.instrumentId
);

if (result.proposalPks.length !== proposalPks.length) {
return rejection('Could not assign proposal/s to instrument', {
agent,
args,
});
}
for await (const proposalPk of proposalPks) {
const technicalReview = await this.reviewDataSource.getTechnicalReview(
proposalPk
);

if (technicalReview) {
await this.proposalDataSource.updateProposalTechnicalReviewer({
userId: instrument.managerUserId,
Expand All @@ -168,6 +181,8 @@ export default class InstrumentMutations {
});
}
}

return result;
}

@ValidateArgs(assignProposalsToInstrumentValidationSchema)
Expand All @@ -186,32 +201,7 @@ export default class InstrumentMutations {
);
}

const instrument = await this.dataSource.getInstrument(args.instrumentId);

if (!instrument) {
return rejection(
'Cannot assign the proposal to the instrument because the proposals call has no such instrument',
{ agent, args }
);
}

const proposalPks = args.proposals.map((proposal) => proposal.primaryKey);

const result = await this.dataSource.assignProposalsToInstrument(
proposalPks,
args.instrumentId
);

if (result.proposalPks.length !== proposalPks.length) {
return rejection('Could not assign proposal/s to instrument', {
agent,
args,
});
}

this.assignProposalsToInstrumentAfterEffect(agent, args);

return result;
return this.assignProposalsToInstrumentHelper(agent, args);
}

@Authorized([Roles.USER_OFFICER])
Expand Down

0 comments on commit f276ba2

Please sign in to comment.