Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hm/twiliov3 #670

Closed
wants to merge 15 commits into from
4 changes: 4 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ STUDENT_PROFILE_URL=http://localhost:3000
# SMTP_HOST=
# SMTP_PASSWORD=
# SMTP_USERNAME=
# TWILIO_ACCOUNT_SID=
# TWILIO_PHONE_NUMBER=
# TWILIO_AUTH_TOKEN=

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"nodemailer": "^6.9.13",
"pdfjs-dist": "^4.5.136",
"postmark": "^3.0.15",
"twilio": "^5.3.7",
"ua-parser-js": "^1.0.36",
"zod": "^3.20.2"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { onboardingSessionWorker } from './modules/onboarding-sessions/onboardin
import { opportunityWorker } from './modules/opportunities';
import { resumeReviewWorker } from './modules/resume-reviews';
import { slackWorker } from './modules/slack/slack.worker';
import { twilioWorker } from './modules/twilio/twilio.worker';

export { job } from './infrastructure/bull';
export { OAuthCodeState } from './modules/authentication/authentication.types';
Expand Down Expand Up @@ -53,4 +54,5 @@ export function startBullWorkers(): void {
profileWorker.run();
resumeReviewWorker.run();
slackWorker.run();
twilioWorker.run();
}
9 changes: 9 additions & 0 deletions packages/core/src/infrastructure/bull.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const BullQueue = {
RESUME_REVIEW: 'resume_review',
SLACK: 'slack',
STUDENT: 'student',
TWILIO: 'twilio',
} as const;

export type BullQueue = ExtractValue<typeof BullQueue>;
Expand Down Expand Up @@ -647,6 +648,13 @@ export const StudentBullJob = z.discriminatedUnion('name', [
}),
]);

export const TwilioMessagingBullJob = z.discriminatedUnion('name', [
z.object({
data: z.object({}),
name: z.literal('twilio.messaging'),
}),
]);

// Combination

export const BullJob = z.union([
Expand All @@ -666,6 +674,7 @@ export const BullJob = z.union([
ResumeReviewBullJob,
SlackBullJob,
StudentBullJob,
TwilioMessagingBullJob,
]);

// Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const ActivityType = {
UPDATE_EDUCATION_HISTORY: 'update_education_history',
UPDATE_WORK_HISTORY: 'update_work_history',
UPLOAD_PROFILE_PICTURE: 'upload_profile_picture',
TWILIO: 'twilio',
} as const;

export type ActivityType = ExtractValue<typeof ActivityType>;
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/modules/twilio/twilio.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Twilio } from 'twilio';

import { db } from '@oyster/db';

import type { GetBullJobData } from '@/infrastructure/bull.types';

const twilioNumber = 'INSERT HERE';
const accountSid = 'INSERT HERE';
const authToken = 'INSERT HERE';
const client = new Twilio(accountSid, authToken);

export async function sendMessages(_: GetBullJobData<'twilio.messaging'>) {
try {
const studentsPhoneNumbers = await db
.selectFrom('students')
.select(['phoneNumber'])
.execute();

console.log('Fetched phone numbers:', studentsPhoneNumbers); // Log the raw data from the database
const numbers = studentsPhoneNumbers
.map(({ phoneNumber }) => phoneNumber)
.filter(Boolean);

const messagePromises = numbers.map((phoneNumber) =>
client.messages
.create({
body: 'Money Here',
to: `${phoneNumber}`,
from: twilioNumber,
})
.then((message) => console.log(`Message sent to ${message.to}`))
.catch((error) =>
console.error(`Failed to send message to ${phoneNumber}:`, error)
)
);

Promise.all(messagePromises)
.then(() => console.log('All messages processed'))
.catch((err) => console.error('Error in processing messages:', err));
} catch (error) {
console.error('Failed to send messages:', error);
}
}
17 changes: 17 additions & 0 deletions packages/core/src/modules/twilio/twilio.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { match } from 'ts-pattern';

import { registerWorker } from '@/infrastructure/bull';
import { TwilioMessagingBullJob } from '@/infrastructure/bull.types';
import { sendMessages } from './twilio.service';

export const twilioWorker = registerWorker(
'twilio',
TwilioMessagingBullJob,
async (job) => {
return match(job)
.with({ name: 'twilio.messaging' }, ({ data }) => {
return sendMessages(data);
})
.exhaustive();
}
);
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://turborepo.org/schema.json",
"pipeline": {
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["build/**", "dist/**"]
Expand Down
Loading
Loading