CG-Feedback-Monitor/server/db/queries/meetingCategoryRatings.ts

48 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import {
deleteRowsWhereEquals,
getAllRows,
getRowsWhereEquals,
getRowsWhereIn,
insertRow,
updateRow,
} from '../crud'
import type { MeetingCategoryRating, RatingScore } from '../../../src/db/types'
export const listMeetingCategoryRatingsForMeeting = (meetingInstanceId: number): MeetingCategoryRating[] =>
getRowsWhereEquals<MeetingCategoryRating>('meetingCategoryRatings', 'meetingInstanceId', meetingInstanceId)
export const listMeetingCategoryRatingsForMeetings = (meetingInstanceIds: number[]): MeetingCategoryRating[] =>
getRowsWhereIn<MeetingCategoryRating>('meetingCategoryRatings', 'meetingInstanceId', meetingInstanceIds)
export function upsertMeetingCategoryRating(input: {
meetingInstanceId: number
instituteeId: number
categoryId: number
score: RatingScore | null
note?: string
}): number {
const existing = getAllRows<MeetingCategoryRating>('meetingCategoryRatings').find(r =>
r.meetingInstanceId === input.meetingInstanceId &&
r.instituteeId === input.instituteeId &&
r.categoryId === input.categoryId,
)
if (existing?.id != null) {
updateRow('meetingCategoryRatings', existing.id, {
score: input.score,
note: input.note ?? existing.note ?? '',
})
return existing.id
}
return insertRow('meetingCategoryRatings', {
meetingInstanceId: input.meetingInstanceId,
instituteeId: input.instituteeId,
categoryId: input.categoryId,
score: input.score,
note: input.note ?? '',
})
}
export const deleteMeetingCategoryRatingsForMeeting = (meetingInstanceId: number): void => {
deleteRowsWhereEquals('meetingCategoryRatings', 'meetingInstanceId', meetingInstanceId)
}