-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84f38ad
commit e1e0a9a
Showing
17 changed files
with
439 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { errorBoundary } from '@stayradiated/error-boundary' | ||
import { prisma } from './prisma.js' | ||
|
||
type DeleteUserOptions = { | ||
userId: string | ||
} | ||
|
||
const deleteUser = async ( | ||
options: DeleteUserOptions, | ||
): Promise<void | Error> => { | ||
const { userId } = options | ||
return errorBoundary(async () => { | ||
const deletePostItems = prisma.postItem.deleteMany({ | ||
Check failure on line 13 in src/db/delete-user.ts GitHub Actions / Handover Bot
|
||
where: { post: { userId } }, | ||
}) | ||
const deletePosts = prisma.post.deleteMany({ where: { userId } }) | ||
const deleteReminder = prisma.reminder.deleteMany({ | ||
where: { userId }, | ||
}) | ||
const deleteUser = prisma.user.delete({ where: { id: userId } }) | ||
await prisma.$transaction([ | ||
deletePostItems, | ||
deletePosts, | ||
deleteReminder, | ||
deleteUser, | ||
]) | ||
}) | ||
} | ||
|
||
export { deleteUser } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { WebClient } from '@slack/web-api' | ||
import { formatDateAsISODate } from '../../date-utils.js' | ||
import { sendReminderToUser } from './send-reminder-to-user.js' | ||
import { findUsersWhoNeedReminder } from './find-users-who-need-reminder.js' | ||
|
||
type CheckAndRemindUsersOptions = { | ||
web: WebClient | ||
daysSinceLastPostCutOff: number | ||
defaultDailyReminderTime: string | ||
} | ||
const checkAndRemindUsers = async ( | ||
options: CheckAndRemindUsersOptions, | ||
): Promise<void | Error> => { | ||
const { web, daysSinceLastPostCutOff, defaultDailyReminderTime } = options | ||
|
||
const instant = Date.now() | ||
|
||
const userList = await findUsersWhoNeedReminder({ | ||
daysSinceLastPostCutOff, | ||
defaultDailyReminderTime, | ||
}) | ||
if (userList instanceof Error) { | ||
return userList | ||
} | ||
|
||
const result = await Promise.all( | ||
userList.map(async (user) => { | ||
const userDate = formatDateAsISODate({ | ||
instant, | ||
timeZone: user.timeZone, | ||
}) | ||
|
||
const sendReminderToUserResult = await sendReminderToUser({ | ||
web, | ||
user, | ||
userDate, | ||
daysSinceLastPostCutOff, | ||
}) | ||
|
||
return sendReminderToUserResult | ||
}), | ||
) | ||
if (result instanceof Error) { | ||
return result | ||
} | ||
} | ||
|
||
export { checkAndRemindUsers } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { randomUUID } from 'node:crypto' | ||
import { vi, describe, test, expect } from 'vitest' | ||
import { assertOk } from '@stayradiated/error-boundary' | ||
import type { User } from '@prisma/client' | ||
import * as dateFns from 'date-fns' | ||
import { | ||
upsertUser, | ||
upsertPost, | ||
upsertPostItem, | ||
deleteUser, | ||
} from '../../db/index.js' | ||
import { findUsersWhoNeedReminder } from './find-users-who-need-reminder.js' | ||
|
||
const daysSinceLastPostCutOff = 7 | ||
const defaultDailyReminderTime = '15:00' | ||
|
||
// Should not be a weekend | ||
vi.setSystemTime(new Date('2021-02-15T17:00:00.000Z')) | ||
|
||
type Context = { | ||
user: User | ||
} | ||
|
||
const myTest = test.extend<Context>({ | ||
async user({}, use) { | ||
const user = await upsertUser({ | ||
id: randomUUID(), | ||
name: 'Test User xyz', | ||
timeZone: 'UTC', | ||
}) | ||
assertOk(user) | ||
|
||
await use(user) | ||
|
||
const result = await deleteUser({ userId: user.id }) | ||
assertOk(result) | ||
}, | ||
}) | ||
|
||
describe('findUsersWhoNeedReminder', () => { | ||
myTest('person who posted yesterday → true', async ({ user }) => { | ||
const yesterdaysPost = await upsertPost({ | ||
userId: user.id, | ||
title: 'Test Post', | ||
date: dateFns.subDays(new Date(), 1), | ||
}) | ||
assertOk(yesterdaysPost) | ||
|
||
const postItem = await upsertPostItem({ | ||
postId: yesterdaysPost.id, | ||
text: '', | ||
channel: '', | ||
ts: Date.now().toString(), | ||
}) | ||
assertOk(postItem) | ||
|
||
const userList = await findUsersWhoNeedReminder({ | ||
daysSinceLastPostCutOff, | ||
defaultDailyReminderTime, | ||
}) | ||
assertOk(userList) | ||
expect(userList).toHaveLength(1) | ||
expect(userList[0]!.id).toBe(user.id) | ||
}) | ||
|
||
myTest('person with no posts → false', async ({ user }) => { | ||
// Must destructure user so that vitest creates them | ||
expect(user.id).toBeDefined() | ||
|
||
const userList = await findUsersWhoNeedReminder({ | ||
daysSinceLastPostCutOff, | ||
defaultDailyReminderTime, | ||
}) | ||
assertOk(userList) | ||
expect(userList).toHaveLength(0) | ||
}) | ||
|
||
myTest('person with an empty post → false', async ({ user }) => { | ||
// Must destructure user so that vitest creates them | ||
expect(user.id).toBeDefined() | ||
|
||
const yesterdaysPost = await upsertPost({ | ||
userId: user.id, | ||
title: 'Test Post', | ||
date: dateFns.subDays(new Date(), 1), | ||
}) | ||
assertOk(yesterdaysPost) | ||
|
||
const userList = await findUsersWhoNeedReminder({ | ||
daysSinceLastPostCutOff, | ||
defaultDailyReminderTime, | ||
}) | ||
|
||
assertOk(userList) | ||
expect(userList).toHaveLength(0) | ||
}) | ||
|
||
myTest('person with a post from > 7 days ago → false', async ({ user }) => { | ||
// Must destructure user so that vitest creates them | ||
expect(user.id).toBeDefined() | ||
|
||
const oldPost = await upsertPost({ | ||
userId: user.id, | ||
title: 'Test Post', | ||
date: dateFns.subDays(new Date(), 8), | ||
}) | ||
assertOk(oldPost) | ||
|
||
const postItem = await upsertPostItem({ | ||
postId: oldPost.id, | ||
text: '', | ||
channel: '', | ||
ts: Date.now().toString(), | ||
}) | ||
assertOk(postItem) | ||
|
||
const userList = await findUsersWhoNeedReminder({ | ||
daysSinceLastPostCutOff, | ||
defaultDailyReminderTime, | ||
}) | ||
|
||
assertOk(userList) | ||
expect(userList).toHaveLength(0) | ||
}) | ||
|
||
myTest('person with a post from today → false', async ({ user }) => { | ||
// Must destructure user so that vitest creates them | ||
expect(user.id).toBeDefined() | ||
|
||
const todaysPost = await upsertPost({ | ||
userId: user.id, | ||
title: 'Test Post', | ||
date: new Date(), | ||
}) | ||
assertOk(todaysPost) | ||
|
||
const postItem = await upsertPostItem({ | ||
postId: todaysPost.id, | ||
text: '', | ||
channel: '', | ||
ts: Date.now().toString(), | ||
}) | ||
assertOk(postItem) | ||
|
||
const userList = await findUsersWhoNeedReminder({ | ||
daysSinceLastPostCutOff, | ||
defaultDailyReminderTime, | ||
}) | ||
|
||
assertOk(userList) | ||
expect(userList).toHaveLength(0) | ||
}) | ||
}) |
Oops, something went wrong.