Skip to content

Instantly share code, notes, and snippets.

@jpcercal
Created November 22, 2024 18:09
Show Gist options
  • Select an option

  • Save jpcercal/9dd4c1bc90897aa6b2b665e02155fb89 to your computer and use it in GitHub Desktop.

Select an option

Save jpcercal/9dd4c1bc90897aa6b2b665e02155fb89 to your computer and use it in GitHub Desktop.
pipoca-agil
async resolveCycleDuration() {
const defaultCycle = 28;
if (true) {// the user does not have a complete cycle yet, we use the default information
return defaultCycle;
}
// const lastThreeCycles = select nomeDaColuna (first date where cycle starts) from table where users_id = <user-id> and date >= <date> date <= <date> ORDER BY date DESC (because we want to get the latest date first whihch is the closest to the current time as of now)
if (true) { // if the user has only one month available (meaning two cycle), then we take this cycle into account
return (lastThreeCycles[0].nomeDaColuna + defaultCycle + defaultCycle) / 3;
}
if (true) { // if the user has only two month available (meaning two cycle), then we take this cycle into account
return (lastThreeCycles[0].nomeDaColuna + lastThreeCycles[1].nomeDaColuna + defaultCycle) / 3;
}
return (lastThreeCycles[0].nomeDaColuna + lastThreeCycles[1].nomeDaColuna + lastThreeCycles[2].nomeDaColuna) / 3;
}
async resolvePeriodDuration() {
const defaultCycle = 7;
// if you guys want to save this information onto user profile data
if (true) {// the user does not have a complete cycle yet, we use the default information
return defaultCycle;
}
// if you guys want to query it in real time, to get the most accurate information, then do this:
// const result = select abc.startDate, abc.endDate from (subquery with https://stackoverflow.com/questions/32603345/grouping-consecutive-dates-in-postgresql) abc order by abc.startDate DESC LIMIT 1
// const periodDuration = result[date]
if (!periodDuration) {
return defaultCycle
}
return periodDuration;
}
async resolveLastEntryOnDatabaseWhereUserHasInputTheStartOfTheLastPeriod() {
return '2022-11-20'; // SELECT startDate from menstrual_cycle_date where users_id = <userID> and date >= <date> date <= <date> ORDER BY date DESC (because we want to get the latest date first whihch is the closest to the current time as of now)
}
async getForecasting(body: CreateMenstrualPeriodDateDto, userId: number) {
const cycleDuration = await resolveCycleDuration() // e.g 28 (if fallback applies, or user data otherwise)
const periodDuration = await resolvePeriodDuration() // e.g 7 (if fallback applies, or user data otherwise)
lastEntryOnDatabaseWhereUserHasInputTheStartOfTheLastPeriod = await resolveLastEntryOnDatabaseWhereUserHasInputTheStartOfTheLastPeriod()
intiallDate = resolveLastEntryOnDatabaseWhereUserHasInputTheStartOfTheLastPeriod // transform to a luxon object
return {
events: {
periodMenstrual: {
duration: periodDuration,
days: [
intiallDate.plus({days: cycleDuration * loopIndex}).toFormat('xxxx'), // 12 rows, add to a loop
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment