return early when data is not available as expected

This commit is contained in:
m5r
2021-08-02 18:02:49 +08:00
parent 09568ef684
commit 827ed9f1c0
11 changed files with 88 additions and 43 deletions

View File

@ -18,24 +18,31 @@ const Body = z.object({
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ content, to }, context) => {
const customer = await getCurrentCustomer(null, context);
if (!customer || !customer.accountSid || !customer.authToken) {
return;
}
try {
await twilio(customer!.accountSid!, customer!.authToken!).lookups.v1.phoneNumbers(to).fetch();
await twilio(customer.accountSid, customer.authToken).lookups.v1.phoneNumbers(to).fetch();
} catch (error) {
logger.error(error);
return;
}
const customerId = customer!.id;
const customerId = customer.id;
const customerPhoneNumber = await getCustomerPhoneNumber({ customerId }, context);
if (!customerPhoneNumber) {
return;
}
const message = await db.message.create({
data: {
customerId,
to,
from: customerPhoneNumber!.phoneNumber,
from: customerPhoneNumber.phoneNumber,
direction: Direction.Outbound,
status: MessageStatus.Queued,
content: encrypt(content, customer!.encryptionKey),
content: encrypt(content, customer.encryptionKey),
sentAt: new Date(),
},
});