Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async function sendTaskStatusChangeEmails(params: {
where: {
organizationId,
deactivated: false,
user: { role: { not: 'admin' } },
},
select: {
role: true,
Expand Down Expand Up @@ -113,6 +114,7 @@ async function sendTaskStatusChangeEmails(params: {
db,
recipient.email,
'taskAssignments',
organizationId,
);

if (isUnsubscribed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function sendTaskStatusChangeEmails(params: {
where: {
organizationId,
deactivated: false,
user: { role: { not: 'admin' } },
},
select: {
role: true,
Expand Down Expand Up @@ -111,6 +112,7 @@ async function sendTaskStatusChangeEmails(params: {
db,
recipient.email,
'taskAssignments',
organizationId,
);

if (isUnsubscribed) {
Expand Down
21 changes: 2 additions & 19 deletions packages/email/lib/check-unsubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,9 @@ export async function isUserUnsubscribed(
return false;
}

// Platform admins only receive notifications for organizations they own
// Platform admins never receive email notifications
if (user.role === 'admin') {
if (!organizationId || !db.member) {
return true; // No org context — block notifications
}
const adminMemberRecords = await db.member.findMany({
where: {
organizationId,
user: { email },
deactivated: false,
},
select: { role: true },
});
const adminRoles = adminMemberRecords.flatMap((m) =>
m.role.split(',').map((r) => r.trim()),
);
if (!adminRoles.includes('owner')) {
return true; // Not an owner in this org — block notifications
}
// Platform admin IS an owner — fall through to normal notification logic
return true;
}

// If legacy all-or-nothing flag is set, user is unsubscribed from everything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ export const employeeAccessCheck: IntegrationCheck = {

ctx.log(`Fetched ${allUsers.length} total users`);

if (userFilterConfig.targetOrgUnits?.length) {
const ouCounts = new Map<string, number>();
for (const user of allUsers) {
const ou = user.orgUnitPath ?? '/';
ouCounts.set(ou, (ouCounts.get(ou) ?? 0) + 1);
}
ctx.log(
`Filtering to OUs: ${userFilterConfig.targetOrgUnits.join(', ')}. ` +
`User OUs: ${[...ouCounts.entries()].map(([ou, count]) => `${ou} (${count})`).join(', ')}`,
);
}

// Same rules as 2FA check and employee sync (sync.controller.ts)
const activeUsers = filterGoogleWorkspaceUsersForChecks(allUsers, userFilterConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ export const twoFactorAuthCheck: IntegrationCheck = {

ctx.log(`Fetched ${allUsers.length} total users`);

if (userFilterConfig.targetOrgUnits?.length) {
const ouCounts = new Map<string, number>();
for (const user of allUsers) {
const ou = user.orgUnitPath ?? '/';
ouCounts.set(ou, (ouCounts.get(ou) ?? 0) + 1);
}
ctx.log(
`Filtering to OUs: ${userFilterConfig.targetOrgUnits.join(', ')}. ` +
`User OUs: ${[...ouCounts.entries()].map(([ou, count]) => `${ou} (${count})`).join(', ')}`,
);
}

// Org units + sync email filter — same rules as employee sync (sync.controller.ts)
const usersToCheck = filterGoogleWorkspaceUsersForChecks(allUsers, userFilterConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ export const targetOrgUnitsVariable: CheckVariable = {
'/admin/directory/v1/customer/my_customer/orgunits?type=all',
);

const rootOption = { value: '/', label: '/ (Root)' };

if (!response.organizationUnits) {
return [{ value: '/', label: '/ (Root)' }];
return [rootOption];
}

return response.organizationUnits.map((ou) => ({
value: ou.orgUnitPath,
label: `${ou.orgUnitPath} (${ou.name})`,
}));
return [
rootOption,
...response.organizationUnits.map((ou) => ({
value: ou.orgUnitPath,
label: `${ou.orgUnitPath} (${ou.name})`,
})),
];
} catch {
return [{ value: '/', label: '/ (Root)' }];
}
Expand Down
Loading