Skip to content

Commit

Permalink
fix db tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessica Ho authored and Jessica Ho committed Feb 27, 2024
1 parent 38edc5a commit 95b5ded
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 152 deletions.
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const config: PlaywrightTestConfig = {
expect: {
timeout: 3000
},
// timeout: 10000,
timeout: 10000,
testDir: 'tests',
use: {
locale: 'en-US',
Expand Down
11 changes: 3 additions & 8 deletions prisma/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export const PHONES = [
'+12015550121',
'+12015550122',
'+12015550123',
'+12015550124',
'+12015550125',
'+12015550126'
];
export const USERS_WITH_NOTHING = [1, 2, 7];
export const USERS_WITH_EMPTY_HOUSEHOLD = [3, 4];
export const USERS_WITH_ACTIVE_SESSION = [2, 4, 6, 7];
10 changes: 7 additions & 3 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { PrismaClient } from '@prisma/client';
import { USERS_WITH_ACTIVE_SESSION, USERS_WITH_EMPTY_HOUSEHOLD, USERS_WITH_NOTHING } from './constants';
import SeedUtils from './utils';

const prisma = new PrismaClient();

async function main() {
const utils = new SeedUtils(new Date(), prisma);

await utils.deleteAllHouseholds();

await Promise.all([
utils.deleteAllFriendRequests(),
utils.createExpiredLink(1),
...[1, 2].map((userInd) => utils.createUserWithNothing(userInd)),
...[3, 4].map((userInd) => utils.createUserWithEmptyHousehold(userInd)),
...[2, 4, 6].map((userInd) => utils.createActiveSession(userInd)),
...USERS_WITH_NOTHING.map((userInd) => utils.createUserWithNothing(userInd)),
...USERS_WITH_EMPTY_HOUSEHOLD.map((userInd) => utils.createUserWithEmptyHousehold(userInd)),
...USERS_WITH_ACTIVE_SESSION.map((userInd) => utils.createActiveSession(userInd)),
utils.createUserWithEmptyHousehold(5),
utils.createUserWithKid(6),
]);
Expand Down
93 changes: 92 additions & 1 deletion prisma/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default class SeedUtils {
'+12015550123',
'+12015550124',
'+12015550125',
'+12015550126'
'+12015550126',
'+12015550127'
];

constructor(now: Date, prisma: PrismaClient) {
Expand Down Expand Up @@ -100,6 +101,96 @@ export default class SeedUtils {
.catch(() => console.log('No friend request table to delete'));
}

async deleteAllHouseholds() {
await this.#prisma.householdChild
.deleteMany()
.catch(() => console.log('No household child table to delete'));
await this.#prisma.household
.deleteMany()
.catch(() => console.log('No household table to delete'));
}

async deleteUserAndHousehold(phone: string) {
const user = await this.#prisma.user.findUnique({
where: { phone }
});
if (!user || !user.householdId) return;
const { householdId } = user;
// delete all kids
const deleteKids = this.#prisma.householdChild.deleteMany({
where: {
householdId
}
});

// delete household invites this household has issued
const deleteHouseholdInvites = this.#prisma.joinHouseholdRequest.deleteMany({
where: {
householdId
}
});

// delete friend reqs this household has issued
const deleteFriendReqs1 = this.#prisma.friendRequest.deleteMany({
where: {
fromHouseholdId: householdId
}
});

// delete all friends with this household
const deleteFriends1 = this.#prisma.householdConnection.deleteMany({
where: {
householdId
}
});
const deleteFriends2 = this.#prisma.householdConnection.deleteMany({
where: {
friendHouseholdId: householdId
}
});

const householdUsers = await this.#prisma.user.findMany({
where: {
householdId
},
select: {
phone: true
}
});

const householdPhones = householdUsers.map((x) => x.phone);

// delete friend reqs this household has received
const deleteFriendReqs2 = this.#prisma.friendRequest.deleteMany({
where: {
targetPhone: { in: householdPhones }
}
});

// delete all adults
const deleteAdults = this.#prisma.user.deleteMany({
where: {
householdId
}
});

// finally, delete the household
const deleteHousehold = this.#prisma.household.delete({
where: { id: householdId }
});

await this.#prisma.$transaction([
deleteKids,
deleteHouseholdInvites,
deleteFriendReqs1,
deleteFriendReqs2,
deleteFriends1,
deleteFriends2,
deleteAdults,
deleteHousehold
]);
}

async createExpiredLink(userInd: number) {
const expiredLink = {
token: '3e99472f1003794c',
Expand Down
4 changes: 1 addition & 3 deletions src/lib/components/Dashboard/ScheduleTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@
.flex {
display: flex;
}
.border-right {
border-right: 1px solid #dddddd;
}
table {
border-collapse: collapse;
border: 1px solid #dddddd;
Expand Down
50 changes: 29 additions & 21 deletions src/routes/household/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
{/if}
<p class="subtitle">Kids</p>
{#each kids as kid, ind}
<div class="card">
<div class="card kid-card">
<p>{kid.firstName} {kid.lastName ?? ''}</p>
<p class="small-font">Pronouns: {PRONOUNS[kid.pronouns]}</p>
<p class="small-font">Age: {isNaN(kid.age) ? 'N/A' : kid.age}</p>
Expand All @@ -421,26 +421,34 @@
{/each}

<form method="POST" action="/db" id="kid-form" on:submit|preventDefault={addKid}>
<label class="subtitle-2" for="first-name">First Name<span class="red">*</span></label>
<input type="text" name="first-name" required />

<label class="subtitle-2" for="last-name">Last Name</label>
<input type="text" name="last-name" />

<label class="subtitle-2" for="pronouns">Pronouns<span class="red">*</span></label>
<select name="pronouns" required>
<option value="" />
{#each Object.entries(PRONOUNS) as pronoun}
<option value={pronoun[0]}>{pronoun[1]}</option>
{/each}
</select>

<label class="subtitle-2" for="dob">Date of Birth</label>
<input
type="date"
name="dob"
max={`${now.getFullYear()}-${now.getMonth()}-${now.getDay()}`}
/>
<label class="subtitle-2" for="first-name"
>First Name<span class="red">*</span><br />
<input type="text" name="first-name" required />
</label>

<label class="subtitle-2" for="last-name"
>Last Name<br />
<input type="text" name="last-name" />
</label>

<label class="subtitle-2" for="pronouns"
>Pronouns<span class="red">*</span><br />
<select name="pronouns" required>
<option value="" />
{#each Object.entries(PRONOUNS) as pronoun}
<option value={pronoun[0]}>{pronoun[1]}</option>
{/each}
</select>
</label>

<label class="subtitle-2" for="dob"
>Date of Birth<br />
<input
type="date"
name="dob"
max={`${now.getFullYear()}-${now.getMonth()}-${now.getDay()}`}
/>
</label>

<div id="btn-container-2"><button class="text-btn">Save Child</button></div>
</form>
Expand Down
Loading

0 comments on commit 95b5ded

Please sign in to comment.