41 create tenant twilio config #62
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 { 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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue