Skip to content

Commit

Permalink
feat: doesn't send email for action author
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed Sep 18, 2023
1 parent 9413ad4 commit f97b279
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 61 deletions.
24 changes: 22 additions & 2 deletions src/utils/worker/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,31 @@ export function createJob<K extends keyof JobDataMap>(kind: K, { data, priority,
});
}

export function createEmailJob<T extends keyof typeof templates>(template: T, data: Parameters<Templates[T]>[number]) {
type Maper<T extends keyof Templates = keyof Templates> = <Params extends Parameters<Templates[T]>[number]>(
params: Params,
) => Params;

const excludeCurrentUser: Maper = (params) => ({
...params,
to: params.to.filter((email) => email !== params.authorEmail),
});

const mappers = [excludeCurrentUser];

export function createEmailJob<T extends keyof Templates, Params extends Parameters<Templates[T]>[number]>(
template: T,
data: Params,
) {
const mappedParams = mappers.reduce<Params>((acum, mapper) => mapper(acum), data);

if (!mappedParams.to.length) {
return null;
}

return createJob('email', {
data: {
template,
data,
data: mappedParams,
},
});
}
12 changes: 12 additions & 0 deletions src/utils/worker/mail/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface GoalCommentedEmailProps {
title: string;
body: string;
commentId: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -67,6 +68,7 @@ interface GoalStateUpdatedEmailProps {
stateTitleAfter?: string;
shortId: string;
title: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -104,6 +106,7 @@ interface GoalStateUpdatedWithCommentEmailProps {
title: string;
body: string;
commentId: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -149,6 +152,7 @@ interface GoalUpdatedEmailProps {
estimate?: FieldDiff;
priority?: FieldDiff;
};
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -215,6 +219,7 @@ interface GoalArchivedEmailProps {
to: SendMailProps['to'];
shortId: string;
title: string;
authorEmail: string;
author?: string;
}

Expand All @@ -239,6 +244,7 @@ interface GoalAssignedEmailProps {
to: SendMailProps['to'];
shortId: string;
title: string;
authorEmail: string;
author?: string;
}

Expand All @@ -264,6 +270,7 @@ interface GoalUnassignedEmailProps {
to: SendMailProps['to'];
shortId: string;
title: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -291,6 +298,7 @@ interface GoalCreatedEmailProps {
projectTitle: string;
shortId: string;
title: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -326,6 +334,7 @@ interface childProjectCreatedProps {
childTitle: string;
projectKey: string;
projectTitle: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -361,6 +370,7 @@ interface childProjectDeletedProps {
childTitle: string;
projectKey: string;
projectTitle: string;
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -398,6 +408,7 @@ interface ProjectUpdatedEmailProps {
title?: FieldDiff;
description?: FieldDiff;
};
authorEmail: string;
author?: string;
}

Expand Down Expand Up @@ -453,6 +464,7 @@ interface ProjectTransferedProps {
to: SendMailProps['to'];
key: string;
title: string;
authorEmail: string;
author?: string;
}

Expand Down
56 changes: 29 additions & 27 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ export const goal = router({
new Set(
[...actualProject.participants, ...actualProject.watchers, actualProject.activity]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);
Expand All @@ -279,12 +278,14 @@ export const goal = router({
shortId: newGoal._shortId,
title: newGoal.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
}),
createEmailJob('goalAssigned', {
to: [newGoal.owner?.user?.email],
shortId: newGoal._shortId,
title: newGoal.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
}),
]);

Expand Down Expand Up @@ -503,7 +504,6 @@ export const goal = router({
new Set(
[...actualGoal.participants, ...actualGoal.watchers, actualGoal.activity, actualGoal.owner]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);
Expand All @@ -514,6 +514,7 @@ export const goal = router({
title: actualGoal.title,
updatedFields,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});

if (actualGoal.ownerId !== input.owner.id) {
Expand All @@ -523,12 +524,14 @@ export const goal = router({
shortId: _shortId,
title: actualGoal.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
}),
createEmailJob('goalAssigned', {
to: [input.owner.user.email],
shortId: _shortId,
title: actualGoal.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
}),
]);
}
Expand Down Expand Up @@ -628,7 +631,6 @@ export const goal = router({
new Set(
[...actualGoal.participants, ...actualGoal.watchers, actualGoal.activity, actualGoal.owner]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);
Expand All @@ -638,6 +640,7 @@ export const goal = router({
shortId: _shortId,
title: actualGoal.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});

return updatedGoal;
Expand Down Expand Up @@ -739,7 +742,6 @@ export const goal = router({
new Set(
[...actualGoal.participants, ...actualGoal.watchers, actualGoal.activity, actualGoal.owner]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);
Expand All @@ -751,6 +753,7 @@ export const goal = router({
stateTitleAfter: updatedGoal.state?.title,
title: actualGoal.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});

return updatedGoal;
Expand Down Expand Up @@ -853,33 +856,32 @@ export const goal = router({
new Set(
[...actualGoal.participants, ...actualGoal.watchers, actualGoal.activity, actualGoal.owner]
.filter(Boolean)
.filter((p) => p.user?.email !== commentAuthor?.user?.email)
.map((r) => r.user?.email),
),
);

if (recipients.length) {
if (input.stateId) {
await createEmailJob('goalStateUpdatedWithComment', {
to: recipients,
shortId: _shortId,
stateTitleBefore: actualGoal.state?.title,
stateTitleAfter: updatedGoal.state?.title,
title: actualGoal.title,
commentId: newComment.id,
author: newComment.activity.user?.name || newComment.activity.user?.email,
body: newComment.description,
});
} else {
await createEmailJob('goalCommented', {
to: recipients,
shortId: _shortId,
title: actualGoal.title,
commentId: newComment.id,
author: newComment.activity.user?.name || newComment.activity.user?.email,
body: newComment.description,
});
}
if (input.stateId) {
await createEmailJob('goalStateUpdatedWithComment', {
to: recipients,
shortId: _shortId,
stateTitleBefore: actualGoal.state?.title,
stateTitleAfter: updatedGoal.state?.title,
title: actualGoal.title,
commentId: newComment.id,
author: newComment.activity.user?.name || newComment.activity.user?.email,
authorEmail: ctx.session.user.email,
body: newComment.description,
});
} else {
await createEmailJob('goalCommented', {
to: recipients,
shortId: _shortId,
title: actualGoal.title,
commentId: newComment.id,
author: newComment.activity.user?.name || newComment.activity.user?.email,
authorEmail: ctx.session.user.email,
body: newComment.description,
});
}

return newComment;
Expand Down
59 changes: 27 additions & 32 deletions trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,21 +543,19 @@ export const project = router({
new Set(
[parent.activity, ...parent.participants, ...parent.watchers]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);

return recipients.length
? createEmailJob('childProjectCreated', {
to: recipients,
childKey: updatedProject.id,
childTitle: updatedProject.title,
projectKey: parent.id,
projectTitle: parent.title,
author: ctx.session.user.name || ctx.session.user.email,
})
: null;
return createEmailJob('childProjectCreated', {
to: recipients,
childKey: updatedProject.id,
childTitle: updatedProject.title,
projectKey: parent.id,
projectTitle: parent.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});
}),
);
}
Expand All @@ -583,21 +581,19 @@ export const project = router({
new Set(
[parent.activity, ...parent.participants, ...parent.watchers]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);

return recipients.length
? createEmailJob('childProjectDeleted', {
to: recipients,
childKey: updatedProject.id,
childTitle: updatedProject.title,
projectKey: parent.id,
projectTitle: parent.title,
author: ctx.session.user.name || ctx.session.user.email,
})
: null;
return createEmailJob('childProjectDeleted', {
to: recipients,
childKey: updatedProject.id,
childTitle: updatedProject.title,
projectKey: parent.id,
projectTitle: parent.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});
}),
);
}
Expand All @@ -619,20 +615,18 @@ export const project = router({
new Set(
[...project.participants, ...project.watchers, project.activity]
.filter(Boolean)
.filter((p) => p.user?.email !== ctx.session.user.email)
.map((r) => r.user?.email),
),
);

if (recipients.length) {
await createEmailJob('projectUpdated', {
to: recipients,
key: project.id,
title: project.title,
updatedFields,
author: ctx.session.user.name || ctx.session.user.email,
});
}
await createEmailJob('projectUpdated', {
to: recipients,
key: project.id,
title: project.title,
updatedFields,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});

return updatedProject;
} catch (error: any) {
Expand Down Expand Up @@ -739,6 +733,7 @@ export const project = router({
key: project.id,
title: project.title,
author: ctx.session.user.name || ctx.session.user.email,
authorEmail: ctx.session.user.email,
});

return transferedProject;
Expand Down

0 comments on commit f97b279

Please sign in to comment.