all repos — caroster @ 11a23c91cc454c6f511947fd1341a589a1bb109c

[Octree] Group carpool to your event https://caroster.io

backend/src/api/stripe/services/stripe.ts (view raw)

 1import type { Stripe } from "stripe";
 2
 3export default () => ({
 4  async enableModule(stripeEvent: Stripe.CheckoutSessionCompletedEvent) {
 5    const eventUuid = stripeEvent.data.object.client_reference_id;
 6    if (!eventUuid) {
 7      strapi.log.error(
 8        `Can't retrieve event UUID in Stripe webhook. Webhook ID: ${stripeEvent.id}`
 9      );
10      return;
11    }
12
13    if (stripeEvent.data.object.payment_status !== "paid") {
14      strapi.log.error(
15        `Can't enable module for event UUID ${eventUuid} as payment status is not 'paid'. Webhook ID: ${stripeEvent.id}`
16      );
17      return;
18    }
19
20    const moduleProduct = {
21      name: "caroster-plus",
22      notificationType: "EnabledCarosterPlus" as const,
23    };
24
25    const event = await strapi.db.query("api::event.event").findOne({
26      where: { uuid: eventUuid },
27      populate: ["creator"],
28    });
29    if (!event) {
30      strapi.log.error(
31        `Can't retrieve event with UUID ${eventUuid}. Webhook ID: ${stripeEvent.id}`
32      );
33      return;
34    }
35
36    try {
37      const enabledModules = event.enabled_modules
38        ? [...event.enabled_modules, moduleProduct.name]
39        : [moduleProduct.name];
40      await strapi.db.query("api::event.event").update({
41        where: { uuid: eventUuid },
42        data: { enabled_modules: enabledModules, unpaid: false },
43      });
44      strapi.log.info(
45        `Module '${moduleProduct.name}' enabled for event ${eventUuid}`
46      );
47
48      if (event.creator)
49        strapi.entityService.create("api::notification.notification", {
50          data: {
51            type: event.unpaid // unpaid before event update
52              ? "EventCreated"
53              : moduleProduct.notificationType,
54            event,
55            user: event.creator,
56          },
57        });
58    } catch (error) {
59      strapi.log.error(
60        `Can't enable module ${moduleProduct.name} for event ${eventUuid}: ${error}`
61      );
62    }
63  },
64});