load twilio config from db

This commit is contained in:
Benjamin Palko 2025-01-26 23:08:06 -05:00
parent 116fcb88d2
commit 1afefa78d1
3 changed files with 19 additions and 9 deletions

View file

@ -1,4 +0,0 @@
import { TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN } from '$env/static/private';
import twilio from 'twilio';
export const TwilioClient = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);

View file

@ -1 +0,0 @@
export * from './client';

View file

@ -1,9 +1,8 @@
import { TWILIO_PHONE_NUMBER } from '$env/static/private';
import { PhoneRegex } from '$lib/regex';
import { logger } from '$lib/server/logger';
import { prisma } from '$lib/server/prisma/index.js';
import { TwilioClient } from '$lib/server/twilio';
import { fail, type Actions } from '@sveltejs/kit';
import twilio from 'twilio';
import zod from 'zod';
export const load = async (event) => {
@ -63,11 +62,27 @@ export const actions = {
return fail(400, { error: 'invalid_message' });
}
const tenant = await prisma.tenantConfig.findUnique({
where: {
tenantId: event.locals.tenant.id,
},
select: {
twilioConfig: true,
},
});
const config = tenant?.twilioConfig;
if (!config) {
return fail(307, { error: 'no_twilio_config' });
}
const client = twilio(config.accountSID, config.authToken);
try {
const result = await TwilioClient.messages.create({
const result = await client.messages.create({
to: phone,
body: message,
from: TWILIO_PHONE_NUMBER,
from: config.phoneNumber,
});
logger.debug(result);
} catch (e) {