Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RafidMuhymin committed Jan 21, 2024
2 parents a740b05 + 5208bbf commit 21f1f90
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/utils/createModuleWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export default function ({ pathname: endpointPathname, methods }) {
});

stringifiedRequest = JSON.stringify({
SECURE_WORKER_ID: methodHandler.SECURE_WORKER_ID,
url,
method,
headers,
Expand Down
1 change: 0 additions & 1 deletion src/workers/dilmahtea-me-email/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async function handlePOST(request: Request, env: ENV) {
}

handlePOST.retry = true;
handlePOST.SECURE_WORKER_ID = "EMAIL";

export default createModuleWorker({
pathname: "*",
Expand Down
38 changes: 34 additions & 4 deletions src/workers/dilmahtea-me-exact-account/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import fetchExactAPI from "../../../utils/fetchExactAPI";
import getCustomerFilter from "../../../utils/getCustomerFilter";
import createModuleWorker, { reply } from "../../../utils/createModuleWorker";

declare interface Body {
declare interface ContactUpdate {
ProviderId: string;
contact: string;
exact_account_guid: string;
exact_contact_guid: string;
}

declare interface Customer {
userId?: string;
Email?: string;
Phone?: string;
Expand All @@ -24,9 +31,32 @@ declare interface Body {
};
}

async function handlePUT(request: Request, env: ENV) {
const { ProviderId, contact, exact_account_guid, exact_contact_guid } =
await request.json<ContactUpdate>();

if (
request.headers.get("x-cf-secure-worker-token") !==
env.CF_SECURE_WORKER_TOKEN
) {
return reply({ success: false, error: "Unauthorized" }, 401);
}

await Promise.all([
fetchExactAPI("PUT", "/crm/Accounts(guid'" + exact_account_guid + "')", {
[ProviderId]: contact,
}),
fetchExactAPI("PUT", "/crm/Contacts(guid'" + exact_contact_guid + "')", {
[ProviderId]: contact,
}),
]);

return reply({ success: true }, 200);
}

async function handlePOST(request: Request, env: ENV) {
const { userId, Email, Phone, FirstName, LastName, Language, Address } =
await request.json<Body>();
await request.json<Customer>();

if (
request.headers.get("x-cf-secure-worker-token") !==
Expand Down Expand Up @@ -91,9 +121,9 @@ async function handlePOST(request: Request, env: ENV) {
}

handlePOST.retry = true;
handlePOST.SECURE_WORKER_ID = "EXACT_ACCOUNT";
handlePUT.retry = true;

export default createModuleWorker({
pathname: "*",
methods: { POST: handlePOST },
methods: { POST: handlePOST, PUT: handlePUT },
});
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ export default async function updateCustomer(
}),
}).then(() => console.log(`Exact: Contact updated`)),
);

return matchedContact;
}

return matchedContact;
} else {
// create a new contact if there is no contact or exact match
const NewContact = await fetchExactAPI("POST", "/CRM/Contacts", {
Expand Down
3 changes: 1 addition & 2 deletions src/workers/dilmahtea-me-retry-workers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export default {
);

if (storedRequest) {
const { SECURE_WORKER_ID, url, method, headers, body } =
storedRequest;
const { url, method, headers, body } = storedRequest;

const retryAttemptURL = new URL(url);

Expand Down
3 changes: 3 additions & 0 deletions src/workers/dilmahtea-me-retry-workers/src/types/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface ENV {
// KV
WORKER_REQUESTS: KVNamespace;

// SERVICES
EMAIL: Fetcher;

// STRIPE
STRIPE_DEV_SIGNING_SECRET_KEY: string;
STRIPE_PROD_SIGNING_SECRET_KEY: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function sendErrorEmail(
body = "Request #" + requestID + " retry completed successfully.";
}

return fetch(env.EMAIL_WORKER_URL, {
return env.EMAIL.fetch(env.EMAIL_WORKER_URL, {
method: "POST",
headers: {
"content-type": "application/json",
Expand Down
1 change: 1 addition & 0 deletions src/workers/dilmahtea-me-retry-workers/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ workers_dev = false
logpush = true
triggers = { crons = ["*/1 * * * *"]}
kv_namespaces = [{ binding = "WORKER_REQUESTS", id = "49c94598e0654db8a2064d4f1874ff26" }]
services = [{ binding = "EMAIL", service = "dilmahtea-me-email" }]

[vars]
FROM_NAME = "Dilmah Europe"
Expand Down

0 comments on commit 21f1f90

Please sign in to comment.