saleor-apps-redis_apl/apps/emails-and-messages/src/modules/trpc/protected-client-procedure-with-services.ts
Krzysztof Wolski 82dfc3fa6f
📧 Dynamic webhook management (#646)
* Make channels section expandable based on override setting

* Revert "Make channels section expandable based on override setting"

This reverts commit e107c5e990b4110156043ed494fb0054bd936654.

* Add status  component

* Remove no longer used component

* Remove no longer used component

* Removed webhook creation during App installation

* Extend tRPC meta to contain webhook sync flag

* Add app baseUrl to the context

* Webhook management service

* Add changeset
2023-06-20 11:38:32 +02:00

57 lines
2.2 KiB
TypeScript

import { createLogger } from "@saleor/apps-shared";
import { createSettingsManager } from "../../lib/metadata-manager";
import { SendgridConfigurationService } from "../sendgrid/configuration/sendgrid-configuration.service";
import { SendgridPrivateMetadataManager } from "../sendgrid/configuration/sendgrid-metadata-manager";
import { SmtpConfigurationService } from "../smtp/configuration/smtp-configuration.service";
import { SmtpPrivateMetadataManager } from "../smtp/configuration/smtp-metadata-manager";
import { syncWebhookStatus } from "../webhook-management/sync-webhook-status";
import { protectedClientProcedure } from "./protected-client-procedure";
import { WebhookManagementService } from "../webhook-management/webhook-management-service";
const logger = createLogger({ name: "protectedWithConfigurationServices middleware" });
/*
* Allow access only for the dashboard users and attaches the
* configuration service to the context.
* The services do not fetch data from the API unless they are used.
* If meta key updateWebhooks is set to true, additional calls to the API will be made
* to create or remove webhooks.
*/
export const protectedWithConfigurationServices = protectedClientProcedure.use(
async ({ next, ctx, meta }) => {
const smtpConfigurationService = new SmtpConfigurationService({
metadataManager: new SmtpPrivateMetadataManager(
createSettingsManager(ctx.apiClient, ctx.appId!),
ctx.saleorApiUrl
),
});
const sendgridConfigurationService = new SendgridConfigurationService({
metadataManager: new SendgridPrivateMetadataManager(
createSettingsManager(ctx.apiClient, ctx.appId!),
ctx.saleorApiUrl
),
});
const result = await next({
ctx: {
smtpConfigurationService,
sendgridConfigurationService,
},
});
if (meta?.updateWebhooks) {
logger.debug("Updating webhooks");
const webhookManagementService = new WebhookManagementService(ctx.baseUrl, ctx.apiClient);
await syncWebhookStatus({
sendgridConfigurationService,
smtpConfigurationService,
webhookManagementService,
});
}
return result;
}
);