1- import appDataSource from '@utils/init_datasource' ;
2- import { ServiceSession , AttendanceStatus } from '@db/entities' ;
3- import xlsx , { WorkSheet } from 'node-xlsx' ;
1+ import {
2+ AttendanceExportsResult ,
3+ AttendanceExportsXLSX ,
4+ AttendanceQueryExportsConditions ,
5+ ExportsModelImpl ,
6+ staticImplements ,
7+ } from './types' ;
8+ import { BaseExportsModel } from './exports_base' ;
9+ import { ServiceSession , type AttendanceStatus } from '@db/entities' ;
410import { HTTPErrors } from '@utils/errors' ;
11+ import { WorkSheet } from 'node-xlsx' ;
12+ import appDataSource from '@utils/init_datasource' ;
513
6- type ExportsResult = {
7- service_session_id : number ;
8- start_time : string ;
9- end_time : string ;
10- service : {
11- name : string ;
12- service_id : number ;
13- } ;
14- service_session_users : {
15- service_session_id : number ;
16- username : string ;
17- ad_hoc : boolean ;
18- attended : AttendanceStatus ;
19- is_ic : boolean ;
20- } [ ] ;
21- } ;
22-
23- type ExportsXLSX = [ [ 'username' , ...string [ ] ] , ...[ string , ...( AttendanceStatus | null ) [ ] ] [ ] ] ;
24-
25- type QueryExportsConditions = {
26- id : number ;
27- } & (
28- | {
29- start_date : string ; // ISO strings, we have already validated this
30- end_date : string ;
31- }
32- | {
33- start_date ?: never ;
34- end_date ?: never ;
35- }
36- ) ;
37-
38- export class ExportsModel {
39- private static getSheetOptions = ( ret : ExportsXLSX ) => ( {
40- '!cols' : [ { wch : 24 } , ...Array ( ret . length ) . fill ( { wch : 16 } ) ] ,
41- } ) ;
42- private static constructXLSX = ( ...data : Parameters < typeof xlsx . build > [ 0 ] ) => xlsx . build ( data ) ;
43-
44- public static async queryExports ( { id, start_date, end_date } : QueryExportsConditions ) {
45- let res : ExportsResult [ ] ;
14+ @staticImplements < ExportsModelImpl > ( )
15+ export class AttendanceExportsModel extends BaseExportsModel {
16+ public static async queryExports ( { id, start_date, end_date } : AttendanceQueryExportsConditions ) {
17+ let res : AttendanceExportsResult [ ] ;
4618 if ( start_date === undefined || end_date === undefined ) {
4719 res = await appDataSource . manager
4820 . createQueryBuilder ( )
@@ -82,16 +54,16 @@ export class ExportsModel {
8254 return res ;
8355 }
8456
85- public static async formatXLSX ( conds : QueryExportsConditions ) {
57+ public static async formatXLSX ( conds : AttendanceQueryExportsConditions ) {
8658 const ret = await this . queryExports ( conds ) ;
8759
8860 if ( ret . length === 0 ) throw HTTPErrors . RESOURCE_NOT_FOUND ;
8961
9062 // create headers
9163 // start_time is in ascending order
92- const headers : ExportsXLSX [ 0 ] = ( [ 'username' ] as ExportsXLSX [ 0 ] ) . concat (
64+ const headers = ( [ 'username' ] as AttendanceExportsXLSX [ 0 ] ) . concat (
9365 ret . map ( ( { start_time } ) => start_time ) ,
94- ) as ExportsXLSX [ 0 ] ;
66+ ) as AttendanceExportsXLSX [ 0 ] ;
9567
9668 // output needs to be in the form:
9769 // [username, [attendance status]]
@@ -118,15 +90,13 @@ export class ExportsModel {
11890 } ) ;
11991 } ) ;
12092
121- const body : ExportsXLSX [ 1 ] [ ] = Object . entries ( usernameMap ) . map ( ( [ username , attendance ] ) => [
122- username ,
123- ...attendance ,
124- ] ) ;
93+ const body : AttendanceExportsXLSX [ 1 ] [ ] = Object . entries ( usernameMap ) . map (
94+ ( [ username , attendance ] ) => [ username , ...attendance ] ,
95+ ) ;
12596
126- const out : ExportsXLSX = [ headers , ...body ] ;
97+ const out : AttendanceExportsXLSX = [ headers , ...body ] ;
12798
12899 const sheetOptions = this . getSheetOptions ( out ) ;
129- console . log ( sheetOptions ) ;
130100
131101 return { name : ret [ 0 ] . service . name , data : out , options : sheetOptions } ;
132102 }
0 commit comments