{"version":3,"file":"webhook-store.mjs","sources":["../../src/services/webhook-store.ts"],"sourcesContent":["/**\n * Webhook store is the implementation of webhook storage over the core_store\n */\n\nimport { errors } from '@strapi/utils';\nimport type { Database } from '@strapi/database';\n\nconst { ValidationError } = errors;\n\nconst webhookModel = {\n  uid: 'webhook',\n  collectionName: 'strapi_webhooks',\n  attributes: {\n    name: {\n      type: 'string',\n    },\n    url: {\n      type: 'text',\n    },\n    headers: {\n      type: 'json',\n    },\n    events: {\n      type: 'json',\n    },\n    enabled: {\n      type: 'boolean',\n    },\n  },\n};\n\ninterface DBInput {\n  name: string;\n  url: string;\n  headers: Record<string, string>;\n  events: string[];\n  enabled: boolean;\n}\n\ninterface DBOutput {\n  id: string;\n  name: string;\n  url: string;\n  headers: Record<string, string>;\n  events: string[];\n  enabled: boolean;\n}\n\nexport interface Webhook {\n  id: string;\n  name: string;\n  url: string;\n  headers: Record<string, string>;\n  events: string[];\n  isEnabled: boolean;\n}\n\nconst toDBObject = (data: Webhook): DBInput => {\n  return {\n    name: data.name,\n    url: data.url,\n    headers: data.headers,\n    events: data.events,\n    enabled: data.isEnabled,\n  };\n};\n\nconst fromDBObject = (row: DBOutput): Webhook => {\n  return {\n    id: row.id,\n    name: row.name,\n    url: row.url,\n    headers: row.headers,\n    events: row.events,\n    isEnabled: row.enabled,\n  };\n};\n\nconst webhookEventValidator = async (allowedEvents: Map<string, string>, events: string[]) => {\n  const allowedValues = Array.from(allowedEvents.values());\n\n  events.forEach((event) => {\n    if (allowedValues.includes(event)) {\n      return;\n    }\n\n    throw new ValidationError(`Webhook event ${event} is not supported`);\n  });\n};\n\nexport interface WebhookStore {\n  allowedEvents: Map<string, string>;\n  addAllowedEvent(key: string, value: string): void;\n  removeAllowedEvent(key: string): void;\n  listAllowedEvents(): string[];\n  getAllowedEvent(key: string): string | undefined;\n  findWebhooks(): Promise<Webhook[]>;\n  findWebhook(id: string): Promise<Webhook | null>;\n  createWebhook(data: Webhook): Promise<Webhook>;\n  updateWebhook(id: string, data: Webhook): Promise<Webhook | null>;\n  deleteWebhook(id: string): Promise<Webhook | null>;\n}\n\nconst createWebhookStore = ({ db }: { db: Database }): WebhookStore => {\n  const webhookQueries = db.query('webhook');\n\n  return {\n    allowedEvents: new Map([]),\n    addAllowedEvent(key, value) {\n      this.allowedEvents.set(key, value);\n    },\n    removeAllowedEvent(key) {\n      this.allowedEvents.delete(key);\n    },\n    listAllowedEvents() {\n      return Array.from(this.allowedEvents.keys());\n    },\n    getAllowedEvent(key) {\n      return this.allowedEvents.get(key);\n    },\n    async findWebhooks() {\n      const results = await webhookQueries.findMany();\n\n      return results.map(fromDBObject);\n    },\n    async findWebhook(id) {\n      const result = await webhookQueries.findOne({ where: { id } });\n      return result ? fromDBObject(result) : null;\n    },\n    async createWebhook(data) {\n      await webhookEventValidator(this.allowedEvents, data.events);\n\n      return webhookQueries\n        .create({\n          data: toDBObject({ ...data, isEnabled: true }),\n        })\n        .then(fromDBObject);\n    },\n    async updateWebhook(id, data) {\n      await webhookEventValidator(this.allowedEvents, data.events);\n\n      const webhook = await webhookQueries.update({\n        where: { id },\n        data: toDBObject(data),\n      });\n\n      return webhook ? fromDBObject(webhook) : null;\n    },\n    async deleteWebhook(id) {\n      const webhook = await webhookQueries.delete({ where: { id } });\n      return webhook ? fromDBObject(webhook) : null;\n    },\n  };\n};\n\nexport { webhookModel, createWebhookStore };\n"],"names":[],"mappings":";AAOA,MAAM,EAAE,gBAAoB,IAAA;AAE5B,MAAM,eAAe;AAAA,EACnB,KAAK;AAAA,EACL,gBAAgB;AAAA,EAChB,YAAY;AAAA,IACV,MAAM;AAAA,MACJ,MAAM;AAAA,IACR;AAAA,IACA,KAAK;AAAA,MACH,MAAM;AAAA,IACR;AAAA,IACA,SAAS;AAAA,MACP,MAAM;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,MACN,MAAM;AAAA,IACR;AAAA,IACA,SAAS;AAAA,MACP,MAAM;AAAA,IACR;AAAA,EACF;AACF;AA4BA,MAA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