Skip to content

Commit

Permalink
prisma change + repository update
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Feb 16, 2024
1 parent a9cb470 commit 1c71948
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Warnings:
- You are about to drop the column `date` on the `AvailabilityDate` table. All the data in the column will be lost.
- A unique constraint covering the columns `[householdId,month,day]` on the table `AvailabilityDate` will be added. If there are existing duplicate values, this will fail.
- Added the required column `day` to the `AvailabilityDate` table without a default value. This is not possible if the table is not empty.
- Added the required column `month` to the `AvailabilityDate` table without a default value. This is not possible if the table is not empty.
*/
-- DropIndex
DROP INDEX "AvailabilityDate_householdId_date_key";

-- AlterTable
ALTER TABLE "AvailabilityDate" DROP COLUMN "date",
ADD COLUMN "day" INTEGER NOT NULL,
ADD COLUMN "month" INTEGER NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "AvailabilityDate_householdId_month_day_key" ON "AvailabilityDate"("householdId", "month", "day");
32 changes: 28 additions & 4 deletions src/lib/server/dbRoutes/upsertDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ import { error } from '@sveltejs/kit';
import { dateNotes } from '../sanitize';
import AvailabilityDateRepository from '../repository/AvailabilityDate';

function validateMonthDay(monthDay: string) {
const monthDayParts = monthDay.split('/');
// TODO: validate this in a better way
if (monthDayParts.length !== 2) throw error(400, "monthDay isn't in m/d form");
let month, day;
try {
month = parseInt(monthDayParts[0]);
day = parseInt(monthDayParts[1]);
} catch (err) {
console.error(err);
throw error(400, "Month and day can't be parsed from monthDay");
}

if (!(1 <= month && month <= 12) || !(1 <= day && day <= 31)) {
throw error(400, 'Invalid month day');
}

return { month, day };
}

export default async function upsertDate(
req: {
monthDay: string;
Expand Down Expand Up @@ -32,9 +52,12 @@ export default async function upsertDate(
const emoticons = isAvailable ? req.emoticons : undefined;
const startTime = isAvailable ? new Date(req.startTime) : undefined;
const endTime = isAvailable ? new Date(req.endTime) : undefined;
const date = new Date(monthDay);

const { month, day } = validateMonthDay(monthDay);

const res = {
date,
month,
day,
status,
notes,
emoticons,
Expand All @@ -44,9 +67,10 @@ export default async function upsertDate(

if (status === AvailabilityStatus.UNSPECIFIED) {
await AvailabilityDateRepository.delete({
householdId_date: {
householdId_month_day: {
householdId,
date
month,
day
}
});
return res;
Expand Down
14 changes: 9 additions & 5 deletions src/lib/server/repository/AvailabilityDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ export default class AvailabilityDateRepository {

static async upsert({
householdId,
date,
month,
day,
status,
notes,
emoticons,
startTime,
endTime
}: {
householdId: number;
date: Date | string;
month: number;
day: number;
status: AvailabilityStatus;
notes: string;
emoticons: string | undefined;
Expand All @@ -37,9 +39,10 @@ export default class AvailabilityDateRepository {
}) {
return await prisma.availabilityDate.upsert({
where: {
householdId_date: {
householdId_month_day: {
householdId,
date
month,
day
}
},
update: {
Expand All @@ -51,7 +54,8 @@ export default class AvailabilityDateRepository {
},
create: {
householdId,
date,
month,
day,
status,
notes,
emoticons,
Expand Down

0 comments on commit 1c71948

Please sign in to comment.