load twilio config from db
This commit is contained in:
parent
116fcb88d2
commit
1afefa78d1
3 changed files with 19 additions and 9 deletions
|
|
@ -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);
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
export * from './client';
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
import { TWILIO_PHONE_NUMBER } from '$env/static/private';
|
|
||||||
import { PhoneRegex } from '$lib/regex';
|
import { PhoneRegex } from '$lib/regex';
|
||||||
import { logger } from '$lib/server/logger';
|
import { logger } from '$lib/server/logger';
|
||||||
import { prisma } from '$lib/server/prisma/index.js';
|
import { prisma } from '$lib/server/prisma/index.js';
|
||||||
import { TwilioClient } from '$lib/server/twilio';
|
|
||||||
import { fail, type Actions } from '@sveltejs/kit';
|
import { fail, type Actions } from '@sveltejs/kit';
|
||||||
|
import twilio from 'twilio';
|
||||||
import zod from 'zod';
|
import zod from 'zod';
|
||||||
|
|
||||||
export const load = async (event) => {
|
export const load = async (event) => {
|
||||||
|
|
@ -63,11 +62,27 @@ export const actions = {
|
||||||
return fail(400, { error: 'invalid_message' });
|
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 {
|
try {
|
||||||
const result = await TwilioClient.messages.create({
|
const result = await client.messages.create({
|
||||||
to: phone,
|
to: phone,
|
||||||
body: message,
|
body: message,
|
||||||
from: TWILIO_PHONE_NUMBER,
|
from: config.phoneNumber,
|
||||||
});
|
});
|
||||||
logger.debug(result);
|
logger.debug(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue