forked from graphile/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { projectName } from "@app/config"; | ||
import { Task } from "graphile-worker"; | ||
|
||
import { SendEmailPayload } from "./send_email"; | ||
|
||
/* For tracking account actions */ | ||
|
||
type AccountAction = | ||
| "linked_account" // | ||
| "unlinked_account" // | ||
| "changed_password" // | ||
| "reset_password" // | ||
| "added_email" // | ||
| "removed_email"; // | ||
|
||
type UserAuditPayload = | ||
| { | ||
type: "added_email"; | ||
user_id: string; | ||
current_user_id: string; | ||
|
||
/** id */ | ||
extra1: string; | ||
|
||
/** email */ | ||
extra2: string; | ||
} | ||
| { | ||
type: "removed_email"; | ||
user_id: string; | ||
current_user_id: string; | ||
|
||
/** id */ | ||
extra1: string; | ||
|
||
/** email */ | ||
extra2: string; | ||
} | ||
| { | ||
type: "linked_account"; | ||
user_id: string; | ||
current_user_id: string; | ||
|
||
/** service */ | ||
extra1: string; | ||
|
||
/** identifier */ | ||
extra2: string; | ||
} | ||
| { | ||
type: "unlinked_account"; | ||
user_id: string; | ||
current_user_id: string; | ||
|
||
/** service */ | ||
extra1: string; | ||
|
||
/** identifier */ | ||
extra2: string; | ||
} | ||
| { | ||
type: "reset_password"; | ||
user_id: string; | ||
current_user_id: string; | ||
} | ||
| { | ||
type: "change_password"; | ||
user_id: string; | ||
current_user_id: string; | ||
}; | ||
|
||
const task: Task = async (rawPayload, { addJob, withPgClient }) => { | ||
const payload: UserAuditPayload = rawPayload as any; | ||
let subject: string; | ||
let actionDescription: string; | ||
switch (payload.type) { | ||
case "added_email": { | ||
subject = `You added an email to your account`; | ||
actionDescription = `You added the email '${payload.extra2}' to your account.`; | ||
break; | ||
} | ||
case "removed_email": { | ||
subject = `You removed an email from your account`; | ||
actionDescription = `You removed the email '${payload.extra2}' from your account.`; | ||
break; | ||
} | ||
case "linked_account": { | ||
subject = `You linked a third-party OAuth provider to your account`; | ||
actionDescription = `You linked a third-party OAuth provider ('${payload.extra1}') to your account.`; | ||
break; | ||
} | ||
case "unlinked_account": { | ||
subject = `You removed a link between your account and a third-party OAuth provider`; | ||
actionDescription = `You removed a link between your account and a third-party OAuth provider ('${payload.extra1}').`; | ||
break; | ||
} | ||
case "reset_password": { | ||
subject = `You reset your password`; | ||
actionDescription = `You reset your password.`; | ||
break; | ||
} | ||
case "change_password": { | ||
subject = `You changed your password`; | ||
actionDescription = `You changed your password.`; | ||
break; | ||
} | ||
default: { | ||
// Ensure we've handled all cases above | ||
const neverPayload: never = payload; | ||
console.error( | ||
`Audit action '${(neverPayload as any).type}' not understood; ignoring.` | ||
); | ||
return; | ||
} | ||
} | ||
|
||
const { rows: userEmails } = await withPgClient((client) => | ||
client.query<{ | ||
id: string; | ||
user_id: string; | ||
email: string; | ||
is_verified: boolean; | ||
is_primary: boolean; | ||
created_at: Date; | ||
updated_at: Date; | ||
}>( | ||
"select * from app_public.user_emails where user_id = $1 and is_verified is true", | ||
[payload.user_id] | ||
) | ||
); | ||
|
||
if (userEmails.length === 0) { | ||
throw new Error("Could not find emails for this user"); | ||
} | ||
|
||
const emails = userEmails.map((e) => e.email); | ||
|
||
const sendEmailPayload: SendEmailPayload = { | ||
options: { | ||
to: emails, | ||
subject: `[${projectName}] ${subject}`, | ||
}, | ||
template: "account_activity.mjml", | ||
variables: { | ||
actionDescription, | ||
}, | ||
}; | ||
await addJob("send_email", sendEmailPayload); | ||
}; | ||
|
||
export default task; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<mjml> | ||
<mj-head> | ||
<mj-attributes> | ||
<mj-class name="background" background-color="#f5faff" font-size="13px" /> | ||
<mj-class name="bold-text" align="center" font-size="20px" font-family="Helvetica" font-weight="bold" /> | ||
<mj-class name="text" align="center" font-size="16px" font-family="Helvetica" /> | ||
<mj-class name="small-text" align="center" font-size="13px" font-family="Helvetica" /> | ||
<mj-class name="blue-section" background-color="#4680b4" /> | ||
<mj-class name="white-section" background-color="#fff" /> | ||
</mj-attributes> | ||
</mj-head> | ||
|
||
<mj-body mj-class="background"> | ||
<mj-section padding-bottom="20px"></mj-section> | ||
|
||
<mj-section mj-class="blue-section"> | ||
<mj-column width="100%"> | ||
<mj-text mj-class="bold-text" color="#FFF" padding-left="25px" padding-right="25px" padding-bottom="28px" padding-top="28px"> | ||
[[actionDescription]] | ||
</mj-text> | ||
<mj-text mj-class="text" color="#FFF" padding-left="25px" padding-right="25px" padding-bottom="28px" padding-top="28px"> | ||
This email is purely for your information and security, if you performed the action above then no further action is necessary. | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
|
||
<mj-section mj-class="blue-section"> | ||
<mj-column width="100%"> | ||
<mj-text mj-class="text" color="#FFF" padding-left="25px" padding-right="25px" padding-bottom="20px" padding-top="20px"> | ||
Best,<br/> | ||
The [[projectName]] Team | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
|
||
<mj-section> | ||
<mj-column width="100%"> | ||
<mj-text mj-class="small-text" color="#777" padding-left="25px" padding-right="25px" padding-bottom="20px" padding-top="20px"> | ||
[[legalText]] | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
|
||
</mj-body> | ||
</mjml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters