Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions apps/api/src/controllers/applicationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import type {
import { ApplicationStates } from '@pcgl-daco/data-model';
import type { SectionRoutesValues, UpdateEditApplicationRequest } from '@pcgl-daco/validation';

import { getEmailConfig } from '@/config/emailConfig.ts';
import { authConfig } from '@/config/authConfig.ts';
import { getDbInstance } from '@/db/index.js';
import { getGroupEmails } from '@/external/pcglAuthZClient.ts';
import BaseLogger from '@/logger.js';
import { type ApplicationListRequest } from '@/routes/types.js';
import { applicationActionSvc } from '@/service/applicationActionService.ts';
Expand Down Expand Up @@ -677,16 +678,25 @@ export const submitRevision = async ({
const { actionId } = submittedRevision.data;

if (result.data.state === ApplicationStates.DAC_REVISIONS_REQUESTED) {
const {
email: { dacAddress },
} = getEmailConfig;
emailService.sendEmailDacForSubmittedRevisions({
id: application.id,
to: dacAddress,
applicantName: applicant_first_name || 'N/A',
submittedDate: new Date(),
actionId,
});
const accessToken = request.session.account?.accessToken || '';
const emails = await getGroupEmails(
accessToken,
`${authConfig.AUTHZ_GROUP_PREFIX_DAC_CHAIR}:${application.dac_id}`,
);

if (emails.success) {
emails.data.forEach((email) => {
emailService.sendEmailDacForSubmittedRevisions({
id: application.id,
to: email,
applicantName: applicant_first_name || 'N/A',
submittedDate: new Date(),
actionId,
});
});
} else {
logger.error('Failed to retrieve group emails, email to dac members has not been sent', emails.error);
}
} else {
emailService.sendEmailRepForSubmittedRevisions({
id: application.id,
Expand Down Expand Up @@ -1057,18 +1067,25 @@ export const submitApplication = async ({
actionId,
});
} else if (result.data.state === ApplicationStates.INSTITUTIONAL_REP_REVIEW) {
const {
email: { dacAddress },
} = getEmailConfig;

// Send email to DAC for review
emailService.sendEmailDacForReview({
id: application.id,
to: dacAddress,
applicantName: applicant_first_name || 'N/A',
submittedDate: new Date(),
actionId,
});
const accessToken = request.session.account?.accessToken || '';
const emails = await getGroupEmails(
accessToken,
`${authConfig.AUTHZ_GROUP_PREFIX_DAC_CHAIR}:${application.dac_id}`,
);
if (emails.success) {
emails.data.forEach((email) => {
// Send email to DAC for review
emailService.sendEmailDacForReview({
id: application.id,
to: email,
applicantName: applicant_first_name || 'N/A',
submittedDate: new Date(),
actionId,
});
});
} else {
logger.error('Failed to retrieve group emails, email to dac members has not been sent', emails.error);
}

// send email to applicant that application is submitted to DAC
emailService.sendEmailApplicantApplicationSubmitted({
Expand Down
35 changes: 35 additions & 0 deletions apps/api/src/external/pcglAuthZClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import urlJoin from 'url-join';
import { fetchWithRetry } from './fetchWithRetry.ts';
import {
addUserToStudyPermissionResponse,
authzGroupResponseValidation,
authZUserInfo,
ServiceTokenResponse,
type PCGLAddUserToStudyPermissionResponse,
Expand Down Expand Up @@ -219,3 +220,37 @@ export const addUsersToStudyPermission = async (
return failure('SYSTEM_ERROR', message);
}
};

/**
* @param accessToken
* @param group
* @returns returns a list of emails associated with the given group
*/
export const getGroupEmails = async (
accessToken: string,
group: string,
): AsyncResult<string[], 'SYSTEM_ERROR' | 'FORBIDDEN' | 'NOT_FOUND'> => {
try {
const response = await fetchAuthZResource(`/group/${group}`, accessToken);

if (response.status === 204) {
// A "204 No content" response is returned when the user is not registered.
return failure('NOT_FOUND', 'Unable to retrieve user information from the PCGL AuthZ service.');
}

const res = await response.json();

const resultGroup = authzGroupResponseValidation.safeParse(res);

if (!resultGroup.success) {
const message = `AuthZ service returned unexpected when look for groups`;
logger.error(`[AUTHZ]: ${message}`, JSON.stringify(res));
return failure('SYSTEM_ERROR', message);
}

return success(resultGroup.data.emails);
} catch (error) {
logger.error(`[AUTHZ]: Unexpected error while getting user info from the AuthZ service.`, error);
return failure('SYSTEM_ERROR', `Error contacting the PCGL Authorization Service.`);
}
};
6 changes: 6 additions & 0 deletions apps/api/src/external/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,9 @@ export const ServiceTokenResponse = z.object({
token: z.string(),
});
export type ServiceTokenResponse = z.infer<typeof ServiceTokenResponse>;

export const authzGroupResponseValidation = z.object({
user_id: z.array(z.string()),
emails: z.array(z.string()),
});
export type PCGLAuthZGroupResponse = z.infer<typeof authzGroupResponseValidation>;