diff --git a/prisma/seed.ts b/prisma/seed.ts index 5b8aaee..ac305f7 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -300,73 +300,6 @@ async function main() { update: session6, create: session6 }); - - const firstDate = DateTime.fromObject( - { - year: 2001, - month: 8, - day: 11 - }, - { - zone: 'utc' - } - ); - - - const firstDateBase = { - status: AvailabilityStatus.AVAILABLE, - notes: 'first date', - emoticons: '', - startTime: firstDate.set({ hour: 14 }).toJSDate(), - endTime: firstDate.set({ hour: 15 }).toJSDate() - }; - await prisma.availabilityDate.upsert({ - where: { - householdId_date: { - householdId: 3, - date: firstDate.toJSDate(), - } - }, - update: firstDateBase, - create: { - householdId: 3, - date: firstDate.toJSDate(), - ...firstDateBase - } - }); - - const lastDate = DateTime.fromObject( - { - year: 2001, - month: 3, - day: 8 - }, - { - zone: 'utc' - } - ); - - const lastDateBase = { - status: AvailabilityStatus.AVAILABLE, - notes: 'last date', - emoticons: '', - startTime: lastDate.set({ hour: 14 }).toJSDate(), - endTime: lastDate.set({ hour: 15 }).toJSDate() - }; - await prisma.availabilityDate.upsert({ - where: { - householdId_date: { - householdId: 3, - date: lastDate.toJSDate(), - } - }, - update: lastDateBase, - create: { - householdId: 3, - date: lastDate.toJSDate(), - ...lastDateBase - } - }); } export async function run() { diff --git a/src/lib/components/Calendar/ScheduleTable.svelte b/src/lib/components/Calendar/ScheduleTable.svelte index aeffeaf..c5678cf 100644 --- a/src/lib/components/Calendar/ScheduleTable.svelte +++ b/src/lib/components/Calendar/ScheduleTable.svelte @@ -60,7 +60,8 @@ await requestToMarkOneRow({ status, displayedRow: displayedRows[i], - availableDetails: null + availableDetails: null, + timeZone }); closeEditor(i); dispatch('markedRow'); diff --git a/src/lib/logics/Calendar/Editor/logic.ts b/src/lib/logics/Calendar/Editor/logic.ts index f67e9d6..f594f4e 100644 --- a/src/lib/logics/Calendar/Editor/logic.ts +++ b/src/lib/logics/Calendar/Editor/logic.ts @@ -137,7 +137,8 @@ export const markRowAsAvailable = async ({ startTime, endTime, availRangeParts - } + }, + timeZone }); return { diff --git a/src/lib/logics/_shared/format.ts b/src/lib/logics/_shared/format.ts index c0fcbd9..21f28fa 100644 --- a/src/lib/logics/_shared/format.ts +++ b/src/lib/logics/_shared/format.ts @@ -18,7 +18,7 @@ export const getDisplayedEmoticons = (dbEmoticons: Set | null) => { function getScheduleItem(row: Row) { const busy = row.availRange === AvailabilityStatus.BUSY; const unspecified = row.availRange === AvailabilityStatus.UNSPECIFIED; - + console.log(row.emoticons) const res: ScheduleItem = { ...row, label: '', @@ -85,7 +85,7 @@ export function generateDiffSchedule(ogRows: Row[], rows: Row[]): string[] { export function generateFullScheduleHelper(rows: Row[]) { const schedule: ScheduleItem[] = []; let lastIsBusy = false; - + console.log(rows.slice(0, 5)) rows.forEach((row) => { if (row.availRange === AvailabilityStatus.UNSPECIFIED) { lastIsBusy = false; diff --git a/src/lib/server/dbRoutes/_shared/upsertHousehold.ts b/src/lib/server/dbRoutes/_shared/upsertHousehold.ts new file mode 100644 index 0000000..f4a4dfa --- /dev/null +++ b/src/lib/server/dbRoutes/_shared/upsertHousehold.ts @@ -0,0 +1,31 @@ +import HouseholdRepository from '$lib/server/repository/Household'; +import UserRepository from '$lib/server/repository/User'; + +export default async function upsertHousehold( + name: string, + publicNotes: string, + householdId: number | null, + userId: number +) { + const data = { + name, + publicNotes, + updatedAt: new Date() + }; + + if (householdId) return await HouseholdRepository.update(householdId, data); + + const household = await HouseholdRepository.create(data); + + // then associate user to it + await UserRepository.update( + { + id: userId + }, + { + householdId: household.id + } + ); + + return household; +} diff --git a/src/lib/server/dbRoutes/createKid.ts b/src/lib/server/dbRoutes/createKid.ts index b14c6e8..2661059 100644 --- a/src/lib/server/dbRoutes/createKid.ts +++ b/src/lib/server/dbRoutes/createKid.ts @@ -1,6 +1,7 @@ import type { Pronoun, User } from '@prisma/client'; import { error } from '@sveltejs/kit'; import HouseholdChildRepository from '../repository/HouseholdChild'; +import upsertHousehold from './_shared/upsertHousehold'; export default async function createKid( req: { @@ -12,12 +13,16 @@ export default async function createKid( user: User ) { const { firstName, pronouns, lastName, dateOfBirth } = req; - const { householdId } = user; + let { householdId } = user; // ensure the household exists before adding kid to it if (!householdId) { - throw error(401, { - message: 'Create a household before trying to add a child to it' - }); + const newHousehold = await upsertHousehold( + lastName ? `${lastName} Family` : '', + '', + householdId, + user.id + ); + householdId = newHousehold.id; } const kid = await HouseholdChildRepository.create({ diff --git a/src/lib/server/dbRoutes/upsertDate.ts b/src/lib/server/dbRoutes/upsertDate.ts index 72604c4..a2a7e07 100644 --- a/src/lib/server/dbRoutes/upsertDate.ts +++ b/src/lib/server/dbRoutes/upsertDate.ts @@ -61,6 +61,7 @@ export default async function upsertDate( startTime, endTime }; + console.log('upsertDate', res) if (status === AvailabilityStatus.UNSPECIFIED) { await AvailabilityDateRepository.delete({ diff --git a/src/lib/server/dbRoutes/upsertHousehold.ts b/src/lib/server/dbRoutes/upsertHousehold.ts index 9366fea..3c39678 100644 --- a/src/lib/server/dbRoutes/upsertHousehold.ts +++ b/src/lib/server/dbRoutes/upsertHousehold.ts @@ -1,8 +1,7 @@ import type { User } from '@prisma/client'; -import HouseholdRepository from '../repository/Household'; -import UserRepository from '../repository/User'; +import upsertHousehold from './_shared/upsertHousehold'; -export default async function upsertHousehold( +export default async function upsertHouseholdRoute( req: { name: string; publicNotes: string; @@ -11,25 +10,5 @@ export default async function upsertHousehold( ) { const { name, publicNotes } = req; const { householdId } = user; - const data = { - name, - publicNotes, - updatedAt: new Date() - }; - - if (!householdId) { - const household = await HouseholdRepository.create(data); - - // then associate user to it - await UserRepository.update( - { - id: user.id - }, - { - householdId: household.id - } - ); - } else { - await HouseholdRepository.update(householdId, data); - } + return await upsertHousehold(name, publicNotes, householdId, user.id); } diff --git a/src/lib/server/loaders/dashboard.ts b/src/lib/server/loaders/dashboard.ts index b094783..7f50b9e 100644 --- a/src/lib/server/loaders/dashboard.ts +++ b/src/lib/server/loaders/dashboard.ts @@ -14,7 +14,8 @@ import { generateFullScheduleHelper, getDisplayedEmoticons } from '$lib/logics/_ import generateSchedRows, { putDbDatesInDict } from '$lib/logics/_shared/generateSchedRows'; import type { Row } from '$lib/logics/_shared/types'; import HouseholdConnectionRepository from '$lib/server/repository/HouseholdConnection'; -import { AvailabilityStatus } from '@prisma/client'; +import { AvailabilityStatus, type AvailabilityDate } from '@prisma/client'; +import { getDbDates } from '../_shared/getDbDates'; export const getHousehold = (household: HouseholdWithExtraInfo) => { return { @@ -98,14 +99,12 @@ export const putCircleInfoInDicts = ( circle.forEach((x) => { if (!x) return; if (userHId === x.friendHouseholdId) { - const friendDatesDict = putDbDatesInDict(x.household.AvailabilityDate, timeZone); - circleDatesDict[x.householdId] = generateSchedRows(friendDatesDict, timeZone); + circleDatesDict[x.householdId] = getHouseholdRows(x.household.AvailabilityDate, timeZone); householdsDict[x.householdId] = getHousehold(x.household); return; } - const friendDatesDict = putDbDatesInDict(x.friendHousehold.AvailabilityDate, timeZone); - circleDatesDict[x.friendHouseholdId] = generateSchedRows(friendDatesDict, timeZone); + circleDatesDict[x.friendHouseholdId] = getHouseholdRows(x.friendHousehold.AvailabilityDate, timeZone); householdsDict[x.friendHouseholdId] = getHousehold(x.friendHousehold); }); @@ -145,6 +144,16 @@ export const getOverlapRange = ( return null; }; +export const getCurrUserRows = async (householdId: number, timeZone: string) => { + const dbDates = await getDbDates(householdId, timeZone); + return getHouseholdRows(dbDates, timeZone); +}; + +const getHouseholdRows = (dbDates: AvailabilityDate[], timeZone: string) => { + const userDatesDict = putDbDatesInDict(dbDates, timeZone); + return generateSchedRows(userDatesDict, timeZone); +}; + export const getOverlaps = ( userRows: RowWithDate[], circleDatesDict: { diff --git a/src/lib/server/repository/FriendRequest.ts b/src/lib/server/repository/FriendRequest.ts index 906b30b..5f24ec5 100644 --- a/src/lib/server/repository/FriendRequest.ts +++ b/src/lib/server/repository/FriendRequest.ts @@ -5,6 +5,7 @@ export default class FriendRequestRepository { static async create( data: Prisma.XOR ) { + console.log('create friendReq', data) return await prisma.friendRequest.create({ data }); diff --git a/src/routes/dashboard/+page.server.ts b/src/routes/dashboard/+page.server.ts index 10a6509..e83a64c 100644 --- a/src/routes/dashboard/+page.server.ts +++ b/src/routes/dashboard/+page.server.ts @@ -1,12 +1,9 @@ import type { CircleMember } from '$lib/logics/Dashboard/_shared/types'; import type { ScheduleItem } from '$lib/logics/_shared/format'; -import generateSchedRows, { - putDbDatesInDict -} from '$lib/logics/_shared/generateSchedRows'; -import { getDbDates } from '$lib/server/_shared/getDbDates'; import { convertSchedRowsToDisplayedRows, getCircleMembers, + getCurrUserRows, getOverlaps, putCircleInfoInDicts } from '$lib/server/loaders/dashboard'; @@ -16,10 +13,8 @@ export const load = (async ({ parent }) => { const { user } = await parent(); const userHouseholdId = user.householdId; - const userDbDates = await getDbDates(userHouseholdId, user.timeZone); - const userDatesDict = putDbDatesInDict(userDbDates, user.timeZone); - const allUserRows = generateSchedRows(userDatesDict, user.timeZone); - + const allUserRows = await getCurrUserRows(userHouseholdId, user.timeZone); + const circle = (await getCircleMembers(userHouseholdId, user.timeZone)) as CircleMember[]; const { circleDatesDict, householdsDict } = putCircleInfoInDicts( circle, @@ -29,10 +24,10 @@ export const load = (async ({ parent }) => { const overlaps = getOverlaps(allUserRows, circleDatesDict, user.timeZone); - const displayedCircleDatesDict: { [key: string]: ScheduleItem[] } = {} + const displayedCircleDatesDict: { [key: string]: ScheduleItem[] } = {}; Object.entries(circleDatesDict).forEach(([friendHId, allFriendRows]) => { - displayedCircleDatesDict[friendHId] = convertSchedRowsToDisplayedRows(allFriendRows) - }) + displayedCircleDatesDict[friendHId] = convertSchedRowsToDisplayedRows(allFriendRows); + }); const displayedUserRows = convertSchedRowsToDisplayedRows(allUserRows); diff --git a/tests/calendar.spec.ts b/tests/calendar.spec.ts index f544866..19d8415 100644 --- a/tests/calendar.spec.ts +++ b/tests/calendar.spec.ts @@ -11,3 +11,8 @@ b. BUSY? - Clear btn c. AVAILABLE? */ +/* +create profile +create kid +save basic info +*/ \ No newline at end of file diff --git a/tests/profile.spec.ts b/tests/profile.spec.ts new file mode 100644 index 0000000..e8963fd --- /dev/null +++ b/tests/profile.spec.ts @@ -0,0 +1,43 @@ +import { test, expect } from '@playwright/test'; +import { run } from '../prisma/seed'; + +// const url = 'http://localhost:5173/profile'; +const host = 'http://localhost:5173'; + +test.beforeEach(async () => { + await run(); +}); + +test("User can create new profile with valid phone number", async ({ page, context }) => { + await page.goto('http://localhost:5173'); + + await page.waitForTimeout(3000); + await page.getByRole('textbox').fill('2015550127'); + await page.getByRole('button').click(); + + let token: string, phone: string; + page.on('dialog', async (dialog) => { + const thing = dialog.message().split(' '); + phone = thing[0]; + token = thing[1]; + dialog.accept(); + }); + page.on('console', async (msg) => { + const first = await msg.args()[0]?.jsonValue(); + if (first === 'PHONE_TOKEN') { + phone = await msg.args()[1].jsonValue(); + token = await msg.args()[2].jsonValue(); + } + }); + await new Promise((resolve) => { + let intervalId = setInterval(() => { + if (phone && token) { + clearInterval(intervalId); + resolve(); + } + }, 100); + }); + await page.goto(`http://localhost:5173/login/${phone!}/${token!}`); + await page.mainFrame().waitForLoadState(); + await expect(page).toHaveURL(host + '/profile'); +})