(fastify: FastifyInstance)
| 60 | |
| 61 | // eslint-disable-next-line @typescript-eslint/require-await |
| 62 | export default async function contentController(fastify: FastifyInstance) { |
| 63 | fastify.withTypeProvider<TypeBoxTypeProvider>().post( |
| 64 | "/templates/render", |
| 65 | { |
| 66 | schema: { |
| 67 | description: "Render message template.", |
| 68 | tags: ["Content"], |
| 69 | body: RenderMessageTemplateRequest, |
| 70 | response: { |
| 71 | 200: RenderMessageTemplateResponse, |
| 72 | 500: BaseMessageResponse, |
| 73 | }, |
| 74 | }, |
| 75 | }, |
| 76 | async (request, reply) => { |
| 77 | const { |
| 78 | contents, |
| 79 | workspaceId, |
| 80 | subscriptionGroupId, |
| 81 | channel, |
| 82 | userProperties, |
| 83 | tags, |
| 84 | } = request.body; |
| 85 | const tagsWithMessageId: MessageTags = { |
| 86 | ...(tags ?? {}), |
| 87 | messageId: tags?.messageId ?? randomUUID(), |
| 88 | }; |
| 89 | |
| 90 | const secretNames = [SecretNames.Subscription]; |
| 91 | if (channel === ChannelType.Webhook) { |
| 92 | secretNames.push(SecretNames.Webhook); |
| 93 | } |
| 94 | |
| 95 | const secrets = ( |
| 96 | await db().query.secret.findMany({ |
| 97 | where: and( |
| 98 | eq(schema.secret.workspaceId, workspaceId), |
| 99 | inArray(schema.secret.name, secretNames), |
| 100 | ), |
| 101 | }) |
| 102 | ).reduce((acc, secret) => { |
| 103 | acc.set(secret.name, secret); |
| 104 | return acc; |
| 105 | }, new Map<string, Secret>()); |
| 106 | |
| 107 | const templateSecrets: Record<string, string> = {}; |
| 108 | const subscriptionSecret = secrets.get(SecretNames.Subscription)?.value; |
| 109 | if (subscriptionSecret) { |
| 110 | templateSecrets[SecretNames.Subscription] = subscriptionSecret; |
| 111 | } |
| 112 | const webhookSecret = secrets.get(SecretNames.Webhook)?.configValue; |
| 113 | if (webhookSecret) { |
| 114 | const validated = schemaValidateWithErr(webhookSecret, WebhookSecret); |
| 115 | if (validated.isErr()) { |
| 116 | return reply.status(500).send({ |
| 117 | message: "Invalid webhook secret configuration", |
| 118 | }); |
| 119 | } |
nothing calls this directly
no test coverage detected