Skip to content

Commit

Permalink
fetch email addresses from strapi
Browse files Browse the repository at this point in the history
  • Loading branch information
RafidMuhymin committed Mar 31, 2024
1 parent 3400496 commit 988b1ba
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/utils/D1Strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ type StrapiCollections = Record<string, any>;

let strapiCollections: StrapiCollections;

export default async function D1Strapi(): Promise<StrapiCollections> {
export default async function D1Strapi(
alternateENV?: typeof env,
): Promise<StrapiCollections> {
if (!strapiCollections) {
const { results } = await (env.USERS as D1Database)
const { results } = await ((env || alternateENV).USERS as D1Database)
.prepare("SELECT * FROM strapi")
.all();

Expand Down
4 changes: 0 additions & 4 deletions src/workers/dilmahtea-me-retry-workers/src/types/env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export interface ENV {
// ENVIRONMENTAL VARIABLES
FROM_EMAIL: string;
FROM_NAME: string;
DEV_EMAIL: string;
SALES_EMAIL: string;
EMAIL_WORKER_URL: string;

// KV
Expand Down
17 changes: 11 additions & 6 deletions src/workers/dilmahtea-me-retry-workers/src/utils/sendErrorEmail.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { ENV, ExtendedError } from "../types";

export default function sendErrorEmail(
import D1Strapi from "../../../../utils/D1Strapi";

export default async function sendErrorEmail(
error: undefined | ExtendedError,
requestID: string,
env: ENV,
) {
let notifySales, subject, body;

const { recurringElement } = await D1Strapi();

const { DEV_EMAIL, SALES_EMAIL } = recurringElement.data.attributes;

if (error) {
const { message, bodyText, responseData } = error;

Expand All @@ -32,17 +38,16 @@ export default function sendErrorEmail(
body = "Request #" + requestID + " retry completed successfully.";
}

return env.EMAIL.fetch(env.EMAIL_WORKER_URL, {
return await env.EMAIL.fetch(env.EMAIL_WORKER_URL, {
method: "POST",
headers: {
"content-type": "application/json",
"x-cf-secure-worker-token": env.CF_SECURE_WORKER_TOKEN,
},
body: JSON.stringify({
to: [
{ email: env.DEV_EMAIL },
notifySales && { email: env.SALES_EMAIL },
].filter(Boolean),
to: [{ email: DEV_EMAIL }, notifySales && { email: SALES_EMAIL }].filter(
Boolean,
),
subject,
content: [{ type: "text/plain", value: body }],
}),
Expand Down
7 changes: 3 additions & 4 deletions src/workers/dilmahtea-me-retry-workers/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ workers_dev = false
logpush = true
triggers = { crons = ["*/1 * * * *"]}
kv_namespaces = [{ binding = "WORKER_REQUESTS", id = "49c94598e0654db8a2064d4f1874ff26" }]
d1_databases = [
{ binding = "USERS", database_name = "users", database_id = "74a1dc02-0705-44fb-b009-fb2fe974e098" }
]
services = [{ binding = "EMAIL", service = "dilmahtea-me-email" }]

[vars]
FROM_NAME = "Dilmah Europe"
FROM_EMAIL = "hello@dilmahtea.me"
DEV_EMAIL = "dev@dilmahtea.me"
SALES_EMAIL = "hello@dilmahtea.me"
EMAIL_WORKER_URL = "https://email.scripts.dilmahtea.me"

# secrets
Expand Down

0 comments on commit 988b1ba

Please sign in to comment.