-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/lib/server/dbRoutes/upsertUnspecifiedDatesAsBusy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { AvailabilityStatus, type User } from '@prisma/client'; | ||
import { error } from '@sveltejs/kit'; | ||
import { dateNotes } from '../sanitize'; | ||
import AvailabilityDateRepository from '../repository/AvailabilityDate'; | ||
|
||
export default async function upsertUnspecifiedDatesAsBusy( | ||
req: { | ||
}, | ||
user: User | ||
) { | ||
const { householdId } = user; | ||
if (!householdId) { | ||
throw error(401, { | ||
message: 'You need to create / join a household before saving a schedule' | ||
}); | ||
} | ||
|
||
|
||
[...Array(21).keys()].forEach((x) => { | ||
const date = new Date(new Date().setDate(now.getDate() + x)); | ||
const englishDay = DAYS[date.getDay()]; | ||
const monthDay = `${date.getMonth() + 1}/${date.getDate()}`; | ||
|
||
if (allAvailableDates && monthDay in allAvailableDates) { | ||
const availRange = allAvailableDates[monthDay].availRange; | ||
|
||
if (availRange === 'Busy') { | ||
if (lastIsBusy) { | ||
const lastEntry = res[res.length - 1].availRange; | ||
// if dateRange has multiple days, then just change the last date | ||
// otherwise just append new date to last one with a hyphen | ||
const separator = '-'; | ||
if (lastEntry.includes(separator)) { | ||
const dates = lastEntry.split(separator); | ||
dates[dates.length - 1] = `${englishDay} ${monthDay}`; | ||
res[res.length - 1].availRange = dates.join(separator); | ||
} else { | ||
res[res.length - 1].availRange += `${separator}${englishDay} ${monthDay}`; | ||
} | ||
} else { | ||
lastIsBusy = true; | ||
res.push({ | ||
status: 'Busy', | ||
availRange: `${englishDay} ${monthDay}` | ||
}); | ||
} | ||
} else { | ||
let emoticons = ''; | ||
const { startHr, startMin, endHr, endMin } = destructRange(availRange) as { | ||
[key: string]: number; | ||
}; | ||
const notes = allAvailableDates[monthDay].notes; | ||
if (allAvailableDates[monthDay].emoticons) { | ||
for (const emoji of allAvailableDates[monthDay].emoticons.split(',')) { | ||
emoticons += EMOTICONS_REVERSE[emoji]; | ||
} | ||
} | ||
|
||
lastIsBusy = false; | ||
|
||
const payload = { | ||
englishDay, | ||
monthDay, | ||
availRange, | ||
notes, | ||
emoticons: emoticons.length ? emoticons : 'N/A', | ||
startHr, | ||
startMin, | ||
endHr, | ||
endMin, | ||
householdId | ||
}; | ||
if (monthDay in dates) { | ||
dates[monthDay].push(payload); | ||
} else { | ||
dates[monthDay] = [payload]; | ||
} | ||
|
||
res.push({ ...payload, status: 'Available' }); | ||
} | ||
} else { | ||
lastIsBusy = false; | ||
} | ||
}); | ||
// const res = { | ||
// // date, | ||
// status: AvailabilityStatus.BUSY, | ||
// notes: '', | ||
// emoticons: undefined, | ||
// startTime: undefined, | ||
// endTime: undefined | ||
// }; | ||
|
||
// if (status === AvailabilityStatus.BUSY) { | ||
// await AvailabilityDateRepository.upsert({ householdId, ...res }); | ||
// return res; | ||
// } | ||
|
||
// // if an entry for this date already exists in the db, then patch it | ||
// // otherwise create it | ||
// const sanitizedNotes = dateNotes(notes ?? ''); | ||
// res.notes = sanitizedNotes; | ||
|
||
// await AvailabilityDateRepository.upsert({ householdId, ...res }); | ||
|
||
// return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters