Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Send data in webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 3, 2019
1 parent 7b9fbfd commit 785befc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.37",
"version": "1.1.38",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -147,5 +147,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.37"
"staart-version": "1.1.38"
}
5 changes: 1 addition & 4 deletions src/crud/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ export const createOrganization = async (organization: Organization) => {
format: "hex"
}).replace("#", "");
organization.profilePicture = `https://ui-avatars.com/api/?name=${encodeURIComponent(
organization.name || "XX"
).replace(
/^([a-zA-Z0-9 _-]+)$/gi,
""
(organization.name || "XX").substring(0, 2).toUpperCase()
)}&background=${backgroundColor}&color=fff`;
return await query(
`INSERT INTO ${tableName("organizations")} ${tableValues(organization)}`,
Expand Down
13 changes: 10 additions & 3 deletions src/helpers/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ export const safeFireWebhook = async (
);
for await (const hook of webhooksToFire) {
try {
await fireSingleWebhook(hook, data);
await fireSingleWebhook(hook, webhook, data);
} catch (error) {}
}
return;
};

const fireSingleWebhook = async (webhook: Webhook, data?: any) => {
const fireSingleWebhook = async (
webhook: Webhook,
hookType: Webhooks,
data?: any
) => {
let secret;
if (webhook.secret)
secret = createHmac("sha1", webhook.secret)
Expand All @@ -47,7 +51,10 @@ const fireSingleWebhook = async (webhook: Webhook, data?: any) => {
"X-Signature": secret,
"Content-Type": webhook.contentType
},
data
data: {
hookType,
data
}
};
const result = await axios.post(webhook.url, options);
if (webhook.id)
Expand Down
54 changes: 36 additions & 18 deletions src/rest/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const updateOrganizationForUser = async (
) => {
if (await can(userId, OrgScopes.UPDATE_ORG, "organization", organizationId)) {
await updateOrganization(organizationId, data);
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION);
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION, data);
trackEvent({ organizationId, type: Webhooks.UPDATE_ORGANIZATION }, locals);
return;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export const updateOrganizationBillingForUser = async (
} else {
result = await createStripeCustomer(organizationId, data);
}
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION_BILLING);
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION_BILLING, data);
trackEvent(
{ organizationId, type: Webhooks.UPDATE_ORGANIZATION_BILLING },
locals
Expand Down Expand Up @@ -340,7 +340,11 @@ export const updateOrganizationSubscriptionForUser = async (
subscriptionId,
data
);
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION_SUBSCRIPTION);
queueWebhook(
organizationId,
Webhooks.UPDATE_ORGANIZATION_SUBSCRIPTION,
data
);
trackEvent(
{ organizationId, type: Webhooks.UPDATE_ORGANIZATION_SUBSCRIPTION },
locals
Expand Down Expand Up @@ -372,7 +376,11 @@ export const createOrganizationSubscriptionForUser = async (
organization.stripeCustomerId,
params
);
queueWebhook(organizationId, Webhooks.CREATE_ORGANIZATION_SUBSCRIPTION);
queueWebhook(
organizationId,
Webhooks.CREATE_ORGANIZATION_SUBSCRIPTION,
params
);
trackEvent(
{ organizationId, type: Webhooks.CREATE_ORGANIZATION_SUBSCRIPTION },
locals
Expand Down Expand Up @@ -415,7 +423,11 @@ export const deleteOrganizationSourceForUser = async (
organization.stripeCustomerId,
sourceId
);
queueWebhook(organizationId, Webhooks.DELETE_ORGANIZATION_SOURCE);
queueWebhook(
organizationId,
Webhooks.DELETE_ORGANIZATION_SOURCE,
sourceId
);
trackEvent(
{ organizationId, type: Webhooks.DELETE_ORGANIZATION_SOURCE },
locals
Expand Down Expand Up @@ -449,7 +461,7 @@ export const updateOrganizationSourceForUser = async (
sourceId,
data
);
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION_SOURCE);
queueWebhook(organizationId, Webhooks.UPDATE_ORGANIZATION_SOURCE, data);
trackEvent(
{ organizationId, type: Webhooks.UPDATE_ORGANIZATION_SOURCE },
locals
Expand Down Expand Up @@ -481,7 +493,7 @@ export const createOrganizationSourceForUser = async (
organization.stripeCustomerId,
card
);
queueWebhook(organizationId, Webhooks.CREATE_ORGANIZATION_SOURCE);
queueWebhook(organizationId, Webhooks.CREATE_ORGANIZATION_SOURCE, card);
trackEvent(
{ organizationId, type: Webhooks.CREATE_ORGANIZATION_SOURCE },
locals
Expand Down Expand Up @@ -750,7 +762,7 @@ export const updateApiKeyForUser = async (
)
) {
const result = await updateApiKey(organizationId, apiKeyId, data);
queueWebhook(organizationId, Webhooks.UPDATE_API_KEY);
queueWebhook(organizationId, Webhooks.UPDATE_API_KEY, data);
trackEvent({ organizationId, type: Webhooks.UPDATE_API_KEY }, locals);
return result;
}
Expand All @@ -772,7 +784,7 @@ export const createApiKeyForUser = async (
)
) {
const result = await createApiKey({ organizationId, ...apiKey });
queueWebhook(organizationId, Webhooks.CREATE_API_KEY);
queueWebhook(organizationId, Webhooks.CREATE_API_KEY, apiKey);
trackEvent({ organizationId, type: Webhooks.CREATE_API_KEY }, locals);
return result;
}
Expand All @@ -794,7 +806,7 @@ export const deleteApiKeyForUser = async (
)
) {
const result = await deleteApiKey(organizationId, apiKeyId);
queueWebhook(organizationId, Webhooks.DELETE_API_KEY);
queueWebhook(organizationId, Webhooks.DELETE_API_KEY, apiKeyId);
trackEvent({ organizationId, type: Webhooks.DELETE_API_KEY }, locals);
return result;
}
Expand Down Expand Up @@ -851,7 +863,7 @@ export const updateDomainForUser = async (
)
) {
const result = await updateDomain(organizationId, domainId, data);
queueWebhook(organizationId, Webhooks.UPDATE_DOMAIN);
queueWebhook(organizationId, Webhooks.UPDATE_DOMAIN, data);
trackEvent({ organizationId, type: Webhooks.UPDATE_DOMAIN }, locals);
return result;
}
Expand Down Expand Up @@ -879,7 +891,7 @@ export const createDomainForUser = async (
...domain,
isVerified: false
});
queueWebhook(organizationId, Webhooks.CREATE_DOMAIN);
queueWebhook(organizationId, Webhooks.CREATE_DOMAIN, domain);
trackEvent({ organizationId, type: Webhooks.CREATE_DOMAIN }, locals);
return result;
}
Expand All @@ -901,7 +913,7 @@ export const deleteDomainForUser = async (
)
) {
const result = await deleteDomain(organizationId, domainId);
queueWebhook(organizationId, Webhooks.DELETE_DOMAIN);
queueWebhook(organizationId, Webhooks.DELETE_DOMAIN, domainId);
trackEvent({ organizationId, type: Webhooks.DELETE_DOMAIN }, locals);
return result;
}
Expand Down Expand Up @@ -936,7 +948,10 @@ export const verifyDomainForUser = async (
const result = await updateDomain(organizationId, domainId, {
isVerified: true
});
queueWebhook(organizationId, Webhooks.VERIFY_DOMAIN);
queueWebhook(organizationId, Webhooks.VERIFY_DOMAIN, {
domainId,
method
});
trackEvent({ organizationId, type: Webhooks.VERIFY_DOMAIN }, locals);
return result;
}
Expand All @@ -949,7 +964,10 @@ export const verifyDomainForUser = async (
const result = await updateDomain(organizationId, domainId, {
isVerified: true
});
queueWebhook(organizationId, Webhooks.VERIFY_DOMAIN);
queueWebhook(organizationId, Webhooks.VERIFY_DOMAIN, {
domainId,
method
});
trackEvent({ organizationId, type: Webhooks.VERIFY_DOMAIN }, locals);
return result;
} else {
Expand Down Expand Up @@ -1011,7 +1029,7 @@ export const updateWebhookForUser = async (
)
) {
const result = await updateWebhook(organizationId, webhookId, data);
queueWebhook(organizationId, Webhooks.UPDATE_WEBHOOK);
queueWebhook(organizationId, Webhooks.UPDATE_WEBHOOK, data);
trackEvent({ organizationId, type: Webhooks.UPDATE_WEBHOOK }, locals);
return result;
}
Expand All @@ -1036,7 +1054,7 @@ export const createWebhookForUser = async (
organizationId,
...webhook
} as Webhook);
queueWebhook(organizationId, Webhooks.CREATE_WEBHOOK);
queueWebhook(organizationId, Webhooks.CREATE_WEBHOOK, webhook);
trackEvent({ organizationId, type: Webhooks.CREATE_WEBHOOK }, locals);
return result;
}
Expand All @@ -1058,7 +1076,7 @@ export const deleteWebhookForUser = async (
)
) {
const result = await deleteWebhook(organizationId, webhookId);
queueWebhook(organizationId, Webhooks.DELETE_WEBHOOK);
queueWebhook(organizationId, Webhooks.DELETE_WEBHOOK, webhookId);
trackEvent({ organizationId, type: Webhooks.DELETE_WEBHOOK }, locals);
return result;
}
Expand Down

0 comments on commit 785befc

Please sign in to comment.