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

Reminder #48

Merged
merged 7 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push, pull_request]
env:
DATABASE_PRISMA_URL: postgres://postgres:postgres@localhost:5432/test
DATABASE_URL: postgres://postgres:postgres@localhost:5432/test
PUBLIC_URL: http://localhost:5173
jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -42,6 +43,9 @@ jobs:
- name: Build in node mode
run: yarn nodebuild

- name: Install playwright
run: npx playwright install

- name: Install Playwright dependencies (just chromium for now)
run: npx playwright install-deps chromium

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"test:unit": "vitest",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write .",
"reset": "prisma migrate reset && yarn dev",
"postinstall": "prisma generate",
"reset": "prisma migrate reset && yarn dev",
"postinstall": "prisma migrate dev",
"build:prev": "vite build && vite preview",
"seed": "prisma db seed"
},
Expand Down
7 changes: 7 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type { Handle, RequestEvent } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client';
import type { User, PhoneContactPermissions } from '@prisma/client';
import * as cron from 'node-cron';
import { sendNotif } from '$lib/server/twilio';

const prisma = new PrismaClient();

import { redirect } from '@sveltejs/kit';
import type { MaybePromise, ResolveOptions } from '@sveltejs/kit/types/internal';

cron.schedule('*/1 * * * *', function () {
sendNotif();
});

const setLocal = async (
user: (User & { phonePermissions: PhoneContactPermissions }) | null,
phone: string,
Expand Down
10 changes: 7 additions & 3 deletions src/lib/server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,14 @@ async function saveUser(
allowInvites,
allowReminders
} = req;
// Get the current date in the user's timezone
const userLocalDate = new Date().toLocaleString('en-US', { timeZone });

const d = new Date();
const day = d.getDay();
const diff = d.getDate() - day + notifStartDay;
// Convert the user's date to a JavaScript Date object
const d = new Date(userLocalDate);

// Calculate the desired date based on the user's timezone
const diff = d.getDate() - d.getDay() + notifStartDay;
d.setDate(diff);
d.setHours(notifHr);
d.setMinutes(notifMin);
Expand Down
66 changes: 64 additions & 2 deletions src/lib/server/twilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,24 @@ const msgToSend = async (
msg = `Thanks for subscribing to reminders and friend availability notifications from ${url}! You can disable this at any time on your Profile page or by responding STOP.`;
break;
}
case 'reminder': {
const { phone } = msgComps;
msg = `Hi! It's your periodic reminder to update your schedule: https://playdate.help/login/${phone}`;
jho44 marked this conversation as resolved.
Show resolved Hide resolved
break;
}
default:
throw error(400, {
message: `Message type ${type} not supported`
});
}
return msg;
};

export const sendMsg = async (request: Request, initiator: User | null) => {
const { phone, sendAt, type, ...rest } = await request.json();
export const sendMsg = async (
request: { phone: string; sendAt?: Date; type: string },
initiator: User | null
) => {
const { phone, sendAt, type, ...rest } = request;
if (!phone || !type) {
throw error(400, {
message: `Missing a ${!phone ? 'phone number' : 'type of message to send'}`
Expand Down Expand Up @@ -229,3 +241,53 @@ export const getMsg = async (url: URL) => {

return response;
};

export async function sendNotif() {
const nowLocal = new Date();
const users = await prisma.user.findMany({
select: {
id: true,
phone: true,
reminderDatetime: true,
reminderIntervalDays: true,
timeZone: true,
phonePermissions: {
select: {
allowReminders: true,
blocked: true
}
}
}
});
users.forEach(async (user) => {
const { id, phone, reminderDatetime, reminderIntervalDays, phonePermissions, timeZone } = user;
const { allowReminders, blocked } = phonePermissions;
if (!allowReminders || blocked) return;

const options = {
timeZone
};

const formattedDate = nowLocal.toLocaleString('en-US', options);
const now = new Date(formattedDate);
const timeDifference = Math.abs(now.getTime() - reminderDatetime.getTime()); // Get the absolute time difference in milliseconds
const minuteInMillis = 60 * 1000; // 1 minute in milliseconds
if (timeDifference < minuteInMillis) {
jho44 marked this conversation as resolved.
Show resolved Hide resolved
// currently within a minute of when user should be reminded
// send notif
await sendMsg({ phone, type: 'reminder' }, null);

// update reminder date for next notif
const newReminderDate = new Date(reminderDatetime);
newReminderDate.setDate(reminderDatetime.getDate() + reminderIntervalDays);
await prisma.user.update({
jho44 marked this conversation as resolved.
Show resolved Hide resolved
where: {
id
},
data: {
reminderDatetime: newReminderDate
}
});
}
});
}
2 changes: 1 addition & 1 deletion src/routes/circle/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
: 'line-through'}"
>
{parent.firstName}
{parent.lastName}
{parent.lastName ?? ''}
</p>
{/each}
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/routes/household/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,15 @@
<h4 class="subtitle-3">Sender</h4>
<p class="subtitle-2" style="display: inline; color: black; font-weight: 500;">
{householdInvites[0].fromUser.firstName}
{householdInvites[0].fromUser.lastName}
{householdInvites[0].fromUser.lastName ?? ''}
</p>
<span style="padding: 0 0.5rem; font-size: 15px;"
>({householdInvites[0].fromUser.phone})</span
>

<h4 class="subtitle-3">Kids</h4>
{#each householdInvites[0].household.children as kid}
<p style="font-size: 18px;">{kid.firstName} {kid.lastName}</p>
<p style="font-size: 18px;">{kid.firstName} {kid.lastName ?? ''}</p>
{/each}
</div>

Expand Down Expand Up @@ -375,7 +375,7 @@
<p class="subtitle">Kids</p>
{#each kids as kid, ind}
<div class="card">
<p>{kid.firstName} {kid.lastName}</p>
<p>{kid.firstName} {kid.lastName ?? ''}</p>
<button class="delete-btn" on:click|preventDefault={() => deleteKid(ind)}><hr /></button>
</div>
<hr class="inner-section" />
Expand Down
4 changes: 2 additions & 2 deletions src/routes/household/[householdId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<p class="subtitle">Kids</p>
{#each household.kids as kid}
<div class="card">
<p>{kid.firstName} {kid.lastName}</p>
<p>{kid.firstName} {kid.lastName ?? ''}</p>
<p class="small-font">Pronouns: {PRONOUNS[kid.pronouns]}</p>
<p class="small-font">Age: {kid.age}</p>
</div>
Expand All @@ -30,7 +30,7 @@
<p class="subtitle">Adults</p>
{#each household.adults as adult}
<div class="card">
<p>{adult.firstName} {adult.lastName}</p>
<p>{adult.firstName} {adult.lastName ?? ''}</p>
<p class="small-font">Pronouns: {PRONOUNS[adult.pronouns]}</p>
<p class="small-font">
Phone: <a href="tel:{adult.phone}">{adult.phone}</a>
Expand Down
6 changes: 3 additions & 3 deletions src/routes/invites/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<p class="household-name">{household.name}</p>
<div>
{#each household.parents as parent}
<p>{parent.firstName} {parent.lastName}</p>
<p>{parent.firstName} {parent.lastName ?? ''}</p>
{/each}
</div>
<a href="tel:{household.phone}">{household.phone}</a>
Expand Down Expand Up @@ -132,12 +132,12 @@
<p class="household-name">{invite.household.name}</p>
<div>
{#each invite.household.parents as parent}
<p>{parent.firstName} {parent.lastName}: {parent.phone}</p>
<p>{parent.firstName} {parent.lastName ?? ''}: {parent.phone}</p>
{/each}
</div>
<div>
{#each invite.household.children as child}
<p>{child.firstName} {child.lastName}</p>
<p>{child.firstName} {child.lastName ?? ''}</p>
{/each}
</div>
<div class="w-full">
Expand Down
77 changes: 0 additions & 77 deletions src/routes/reminder/+server.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/routes/twilio/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export async function POST({
}) {
const sessionToken = cookies.get('session');
const { user } = await getProfileFromSession(sessionToken);

return sendMsg(request, user);
return sendMsg(await request.json(), user);
}

export function GET({ url }: { url: URL }) {
Expand Down