mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 13:28:11 +00:00
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { DataSource } from 'typeorm';
|
|
|
|
import { NotificationsService } from './notifications.service';
|
|
|
|
/**
|
|
* Best-effort SMS + email fan-out to a company's contacts. Looks up the
|
|
* company's phone/email and sends the message over both channels, swallowing
|
|
* per-channel failures so a missing provider never breaks the caller's flow.
|
|
*/
|
|
export async function sendCompanyChannels(
|
|
dataSource: DataSource,
|
|
notifications: NotificationsService,
|
|
companyId: string,
|
|
message: string,
|
|
): Promise<void> {
|
|
const [contact]: Array<{ phone: string | null; email: string | null }> =
|
|
await dataSource.query(
|
|
`SELECT COALESCE(phone, etrade_phone) AS phone, email
|
|
FROM freight.companies
|
|
WHERE id = $1 AND deleted_at IS NULL`,
|
|
[companyId],
|
|
);
|
|
if (contact?.phone) {
|
|
try {
|
|
await notifications.directSend('sms', contact.phone, message);
|
|
} catch {
|
|
/* best-effort: SMS provider unavailable */
|
|
}
|
|
}
|
|
if (contact?.email) {
|
|
try {
|
|
await notifications.directSend('email', contact.email, message);
|
|
} catch {
|
|
/* best-effort: email provider unavailable */
|
|
}
|
|
}
|
|
}
|