Skip to content

Commit

Permalink
create household before kid
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Feb 20, 2024
1 parent ffa373d commit 1cf6087
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/lib/server/dbRoutes/createKid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 +12,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

0 comments on commit 1cf6087

Please sign in to comment.