-
Notifications
You must be signed in to change notification settings - Fork 1
feat(server): implemented evaluation of lesson attendance #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { column, knex, WithOptionalTx } from "@/query"; | ||
import { first, isEmpty } from "lodash"; | ||
import { ISessionEvent } from "@litespace/types"; | ||
import { ISession, ISessionEvent } from "@litespace/types"; | ||
import { Knex } from "knex"; | ||
import dayjs from "@/lib/dayjs"; | ||
|
||
|
@@ -12,6 +12,7 @@ export class SessionEvents { | |
tx?: Knex.Transaction | ||
): Promise<ISessionEvent.Self> { | ||
const now = dayjs.utc().toDate(); | ||
|
||
const rows = await this.builder(tx) | ||
.insert({ | ||
type: event.type, | ||
|
@@ -66,10 +67,12 @@ export class SessionEvents { | |
async find({ | ||
users, | ||
sessionIds, | ||
events, | ||
tx, | ||
}: WithOptionalTx<{ | ||
users?: number[]; | ||
sessionIds?: number[]; | ||
sessionIds?: ISession.Id[]; | ||
events?: ISessionEvent.EventType[]; | ||
}>): Promise<ISessionEvent.Self[]> { | ||
const builder = this.builder(tx).select("*"); | ||
|
||
|
@@ -79,7 +82,10 @@ export class SessionEvents { | |
if (sessionIds && !isEmpty(sessionIds)) | ||
builder.whereIn(this.column("session_id"), sessionIds); | ||
|
||
const rows = await builder.then(); | ||
if (events && !isEmpty(events)) | ||
builder.whereIn(this.column("type"), events); | ||
|
||
const rows = await builder.orderBy("created_at", "asc").then(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return rows.map((row) => this.from(row)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,7 +145,9 @@ export type FindLessonsApiQuery = IFilter.SkippablePagination & { | |
|
||
export type FindUserLessonsApiResponse = Paginated<{ | ||
lesson: Self; | ||
members: PopuldatedMember[]; | ||
members: (PopuldatedMember & { | ||
attended: boolean; | ||
})[]; | ||
Comment on lines
+148
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definding the type this way is confusing. |
||
}>; | ||
|
||
export enum Duration { | ||
|
@@ -168,5 +170,7 @@ export type LessonDays = LessonDay[]; | |
|
||
export type FindLessonByIdApiResponse = { | ||
lesson: Self; | ||
members: PopuldatedMember[]; | ||
members: (PopuldatedMember & { | ||
attended: boolean; | ||
})[]; | ||
Comment on lines
+173
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the type. |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import dayjs from "@/lib/dayjs"; | ||
import { ISessionEvent } from "@litespace/types"; | ||
|
||
const MIN_ATTENDANCE_REQUIRED = 0.25; | ||
|
||
export function evaluateAttendance({ | ||
events, | ||
userIds, | ||
duration, | ||
}: { | ||
events: ISessionEvent.Self[]; | ||
userIds: number[]; | ||
duration: number; | ||
}): Record<number, boolean> { | ||
const attendanceStatus: Record<number, boolean> = Object.fromEntries( | ||
userIds.map((id) => [id, false]) | ||
); | ||
|
||
const userEventsMap = new Map<number, ISessionEvent.Self[]>(); | ||
|
||
events.forEach((event) => { | ||
if (userIds.includes(event.userId)) { | ||
if (!userEventsMap.has(event.userId)) userEventsMap.set(event.userId, []); | ||
|
||
userEventsMap.get(event.userId)?.push(event); | ||
} | ||
}); | ||
|
||
userEventsMap.forEach((events, userId) => { | ||
let totalTime = 0; | ||
let lastJoinTime: number | null = null; | ||
|
||
for (const event of events) { | ||
if (event.type === ISessionEvent.EventType.UserJoined) { | ||
if (lastJoinTime !== null) continue; | ||
|
||
lastJoinTime = dayjs.utc(event.createdAt).valueOf(); | ||
} | ||
|
||
if (event.type === ISessionEvent.EventType.UserLeft) { | ||
if (lastJoinTime === null) continue; | ||
|
||
const leftTime = dayjs.utc(event.createdAt).valueOf(); | ||
totalTime += leftTime - lastJoinTime; | ||
lastJoinTime = null; | ||
} | ||
} | ||
|
||
const minAttendanceMs = MIN_ATTENDANCE_REQUIRED * duration * 60 * 1000; // minimum attendance in ms | ||
|
||
console.log({ minAttendanceMs, totalTime }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. left over. |
||
|
||
attendanceStatus[userId] = totalTime >= minAttendanceMs; | ||
}); | ||
|
||
return attendanceStatus; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining the type this way is confusing.