Skip to content

Commit 0785ffb

Browse files
committed
Change notification recipients to use session ALS
1 parent cc91273 commit 0785ffb

File tree

3 files changed

+68
-56
lines changed

3 files changed

+68
-56
lines changed

src/components/authentication/authentication.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ export class AuthenticationService {
230230
return this.repo.waitForRootUserId();
231231
}
232232

233+
async asUser<R>(
234+
userId: ID<'User'>,
235+
fn: (session: Session) => Promise<R>,
236+
): Promise<R> {
237+
const session = await this.sessionForUser(userId);
238+
return await this.sessionHost.withSession(session, () => fn(session));
239+
}
240+
233241
async sessionForUser(userId: ID): Promise<Session> {
234242
const roles = await this.repo.rolesForUser(userId);
235243
const session: Session = {

src/components/progress-report/workflow/handlers/progress-report-workflow-notification.handler.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,43 @@ export class ProgressReportWorkflowNotificationHandler
113113
languageId: ID,
114114
): Promise<EmailReportStatusNotification> {
115115
const recipientId = receiver.userId ?? this.configService.rootUser.id;
116-
const recipientSession = await this.auth.sessionForUser(recipientId);
116+
return await this.auth.asUser(recipientId, async (recipientSession) => {
117+
const recipient = receiver.userId
118+
? await this.userService.readOne(recipientId, recipientSession)
119+
: this.fakeUserFromEmailAddress(receiver.email!);
117120

118-
const recipient = receiver.userId
119-
? await this.userService.readOne(recipientId, recipientSession)
120-
: this.fakeUserFromEmailAddress(receiver.email!);
121+
const project = await this.projectService.readOne(
122+
projectId,
123+
recipientSession,
124+
);
125+
const language = await this.languageService.readOne(
126+
languageId,
127+
recipientSession,
128+
);
129+
const report = await this.reportService.readOne(
130+
reportId,
131+
recipientSession,
132+
);
133+
const changedBy = await this.userService.readOne(
134+
unsecuredEvent.who.id,
135+
recipientSession,
136+
);
137+
const workflowEvent = this.workflowService.secure(
138+
unsecuredEvent,
139+
recipientSession,
140+
);
121141

122-
const project = await this.projectService.readOne(
123-
projectId,
124-
recipientSession,
125-
);
126-
const language = await this.languageService.readOne(
127-
languageId,
128-
recipientSession,
129-
);
130-
const report = await this.reportService.readOne(reportId, recipientSession);
131-
const changedBy = await this.userService.readOne(
132-
unsecuredEvent.who.id,
133-
recipientSession,
134-
);
135-
const workflowEvent = this.workflowService.secure(
136-
unsecuredEvent,
137-
recipientSession,
138-
);
139-
140-
return {
141-
changedBy,
142-
recipient,
143-
project,
144-
language,
145-
report,
146-
newStatusVal: report.status?.value,
147-
previousStatusVal: report.status?.value ? previousStatus : undefined,
148-
workflowEvent: workflowEvent,
149-
};
142+
return {
143+
changedBy,
144+
recipient,
145+
project,
146+
language,
147+
report,
148+
newStatusVal: report.status?.value,
149+
previousStatusVal: report.status?.value ? previousStatus : undefined,
150+
workflowEvent: workflowEvent,
151+
};
152+
});
150153
}
151154

152155
private fakeUserFromEmailAddress(

src/components/project/workflow/handlers/project-workflow-notification.handler.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,31 @@ export class ProjectWorkflowNotificationHandler
104104
primaryPartnerName: string | null,
105105
): Promise<ProjectStepChangedProps> {
106106
const recipientId = notifier.id ?? this.config.rootUser.id;
107-
const recipientSession = await this.auth.sessionForUser(recipientId);
108-
const recipient = notifier.id
109-
? await this.users.readOne(recipientId, recipientSession)
110-
: ({
111-
email: { value: notifier.email, canRead: true, canEdit: false },
112-
displayFirstName: {
113-
value: notifier.email!.split('@')[0],
114-
canRead: true,
115-
canEdit: false,
116-
},
117-
displayLastName: { value: '', canRead: true, canEdit: false },
118-
timezone: {
119-
value: this.config.defaultTimeZone,
120-
canRead: true,
121-
canEdit: false,
122-
},
123-
} satisfies ProjectStepChangedProps['recipient']);
107+
return await this.auth.asUser(recipientId, async (recipientSession) => {
108+
const recipient = notifier.id
109+
? await this.users.readOne(recipientId, recipientSession)
110+
: ({
111+
email: { value: notifier.email, canRead: true, canEdit: false },
112+
displayFirstName: {
113+
value: notifier.email!.split('@')[0],
114+
canRead: true,
115+
canEdit: false,
116+
},
117+
displayLastName: { value: '', canRead: true, canEdit: false },
118+
timezone: {
119+
value: this.config.defaultTimeZone,
120+
canRead: true,
121+
canEdit: false,
122+
},
123+
} satisfies ProjectStepChangedProps['recipient']);
124124

125-
return {
126-
recipient,
127-
changedBy: this.users.secure(changedBy, recipientSession),
128-
project: this.projects.secure(project, recipientSession),
129-
previousStep,
130-
primaryPartnerName,
131-
};
125+
return {
126+
recipient,
127+
changedBy: this.users.secure(changedBy, recipientSession),
128+
project: this.projects.secure(project, recipientSession),
129+
previousStep,
130+
primaryPartnerName,
131+
};
132+
});
132133
}
133134
}

0 commit comments

Comments
 (0)