Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Feb 20, 2024
1 parent 7e60ec4 commit 535aa5a
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 115 deletions.
67 changes: 0 additions & 67 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/Calendar/ScheduleTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
await requestToMarkOneRow({
status,
displayedRow: displayedRows[i],
availableDetails: null
availableDetails: null,
timeZone
});
closeEditor(i);
dispatch('markedRow');
Expand Down
3 changes: 2 additions & 1 deletion src/lib/logics/Calendar/Editor/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export const markRowAsAvailable = async ({
startTime,
endTime,
availRangeParts
}
},
timeZone
});

return {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/logics/_shared/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getDisplayedEmoticons = (dbEmoticons: Set<string> | 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: '',
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions src/lib/server/dbRoutes/_shared/upsertHousehold.ts
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 9 additions & 4 deletions src/lib/server/dbRoutes/createKid.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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({
Expand Down
1 change: 1 addition & 0 deletions src/lib/server/dbRoutes/upsertDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default async function upsertDate(
startTime,
endTime
};
console.log('upsertDate', res)

if (status === AvailabilityStatus.UNSPECIFIED) {
await AvailabilityDateRepository.delete({
Expand Down
27 changes: 3 additions & 24 deletions src/lib/server/dbRoutes/upsertHousehold.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
19 changes: 14 additions & 5 deletions src/lib/server/loaders/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions src/lib/server/repository/FriendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class FriendRequestRepository {
static async create(
data: Prisma.XOR<Prisma.FriendRequestCreateInput, Prisma.FriendRequestUncheckedCreateInput>
) {
console.log('create friendReq', data)
return await prisma.friendRequest.create({
data
});
Expand Down
17 changes: 6 additions & 11 deletions src/routes/dashboard/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions tests/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@
b. BUSY? - Clear btn
c. AVAILABLE?
*/
/*
create profile
create kid
save basic info
*/
43 changes: 43 additions & 0 deletions tests/profile.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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');
})

0 comments on commit 535aa5a

Please sign in to comment.