1+ import {
2+ TaskType ,
3+ RecurrenceFrequency ,
4+ TimeOption ,
5+ DayOfWeek ,
6+ } from "@prisma/client" ;
17import prisma from "../../prisma" ;
2- import { TaskType , RecurrenceFrequency , TimeOption , DayOfWeek } from "@prisma/client" ;
38import { formatDateTime , getToday } from "../../utils/formatDateTime" ;
49
510async function createAssignedTasks ( ) : Promise < boolean > {
611 try {
712 const participants = await prisma . participant . findMany ( {
813 where : {
914 OR : [ { departure_date : null } , { departure_date : { gt : getToday ( ) } } ] ,
10- }
15+ } ,
1116 } ) ;
1217
1318 const requiredTasks = await prisma . task . findMany ( {
1419 where : { task_type : TaskType . REQUIRED } ,
15- } )
20+ } ) ;
1621
1722 const weekdayOffsets : { [ key in DayOfWeek ] : number } = {
1823 MONDAY : 0 ,
@@ -24,86 +29,102 @@ async function createAssignedTasks(): Promise<boolean> {
2429 SUNDAY : 6 ,
2530 } ;
2631
27- const mondayOfCurrentWeek = new Date ( ) ;
32+ const mondayOfCurrentWeek = new Date ( ) ;
33+
34+ await Promise . all (
35+ participants . map ( async ( participant ) => {
36+ return Promise . all (
37+ requiredTasks . map ( async ( task ) => {
38+ if (
39+ task . recurrence_preference ===
40+ RecurrenceFrequency . EVERY_SELECTED_DAYS ||
41+ task . recurrence_preference === RecurrenceFrequency . DAILY
42+ ) {
43+ await Promise . all (
44+ task . repeat_days . map ( async ( repeatDay ) => {
45+ const offset = weekdayOffsets [ repeatDay ] ;
46+ const taskDate = new Date ( mondayOfCurrentWeek ) ;
47+ taskDate . setDate ( mondayOfCurrentWeek . getDate ( ) + offset ) ;
48+ let startDate : Date ;
49+ let endDate : Date ;
50+ if ( task . time_preference === TimeOption . SPECIFIC ) {
51+ startDate = new Date ( taskDate ) ;
52+ endDate = new Date ( taskDate ) ;
53+ if ( task . start_time ) {
54+ const [ hours , minutes ] = task . start_time
55+ . split ( ":" )
56+ . map ( Number ) ;
57+ startDate . setHours ( hours , minutes , 0 , 0 ) ;
58+ }
59+ if ( task . end_time ) {
60+ const [ hours , minutes ] = task . end_time
61+ . split ( ":" )
62+ . map ( Number ) ;
63+ endDate . setHours ( hours , minutes , 0 , 0 ) ;
64+ }
65+ } else {
66+ startDate = new Date ( taskDate ) ;
67+ startDate . setHours ( 0 , 0 , 0 , 0 ) ;
68+ endDate = new Date ( taskDate ) ;
69+ endDate . setHours ( 23 , 59 , 0 , 0 ) ;
70+ }
71+ const startDateString = formatDateTime ( startDate , true ) ;
72+ const endDateString = formatDateTime ( endDate , true ) ;
73+ await prisma . assignedTask . create ( {
74+ data : {
75+ participant_id : participant . participant_id ,
76+ task_name : task . task_name ,
77+ task_type : task . task_type ,
78+ start_date : startDateString ,
79+ end_date : endDateString ,
80+ marillac_bucks_addition : task . marillac_bucks_addition ,
81+ marillac_bucks_deduction : task . marillac_bucks_deduction ,
82+ comment : task . comment ,
83+ } ,
84+ } ) ;
85+ } )
86+ ) ;
87+ } else if (
88+ task . recurrence_preference ===
89+ RecurrenceFrequency . ANY_SELECTED_DAYS
90+ ) {
91+ const firstDayOffset = weekdayOffsets [ task . repeat_days [ 0 ] ] ;
92+ const lastDayOffset =
93+ weekdayOffsets [ task . repeat_days [ task . repeat_days . length - 1 ] ] ;
2894
29- for ( const participant of participants ) {
30- for ( const task of requiredTasks ) {
31- if ( task . recurrence_preference === RecurrenceFrequency . EVERY_SELECTED_DAYS ||
32- task . recurrence_preference === RecurrenceFrequency . DAILY ) {
33- for ( const repeatDay of task . repeat_days ) {
34- const offset = weekdayOffsets [ repeatDay ] ;
35- const taskDate = new Date ( mondayOfCurrentWeek ) ;
36- taskDate . setDate ( mondayOfCurrentWeek . getDate ( ) + offset ) ;
37- let startDate : Date ;
38- let endDate : Date ;
39- if ( task . time_preference === TimeOption . SPECIFIC ) {
40- startDate = new Date ( taskDate ) ;
41- endDate = new Date ( taskDate ) ;
42- if ( task . start_time ) {
43- const [ hours , minutes ] = task . start_time . split ( ":" ) . map ( Number ) ;
44- startDate . setHours ( hours , minutes , 0 , 0 ) ;
45- }
46- if ( task . end_time ) {
47- const [ hours , minutes ] = task . end_time . split ( ":" ) . map ( Number ) ;
48- endDate . setHours ( hours , minutes , 0 , 0 ) ;
49- }
50- } else {
51- startDate = new Date ( taskDate ) ;
95+ const startDate = new Date ( mondayOfCurrentWeek ) ;
96+ startDate . setDate ( mondayOfCurrentWeek . getDate ( ) + firstDayOffset ) ;
5297 startDate . setHours ( 0 , 0 , 0 , 0 ) ;
53- endDate = new Date ( taskDate ) ;
98+
99+ const endDate = new Date ( mondayOfCurrentWeek ) ;
100+ endDate . setDate ( mondayOfCurrentWeek . getDate ( ) + lastDayOffset ) ;
54101 endDate . setHours ( 23 , 59 , 0 , 0 ) ;
55- }
56- const startDateString = formatDateTime ( startDate , true ) ;
57- const endDateString = formatDateTime ( endDate , true ) ;
58- await prisma . assignedTask . create ( {
59- data : {
60- participant_id : participant . participant_id ,
61- task_name : task . task_name ,
62- task_type : task . task_type ,
63- start_date : startDateString ,
64- end_date : endDateString ,
65- marillac_bucks_addition : task . marillac_bucks_addition ,
66- marillac_bucks_deduction : task . marillac_bucks_deduction ,
67- comment : task . comment ,
68- } ,
69- } ) ;
70- }
71- } else if ( task . recurrence_preference === RecurrenceFrequency . ANY_SELECTED_DAYS ) {
72102
73- const firstDayOffset = weekdayOffsets [ task . repeat_days [ 0 ] ] ;
74- const lastDayOffset = weekdayOffsets [ task . repeat_days [ task . repeat_days . length - 1 ] ] ;
75-
76- const startDate = new Date ( mondayOfCurrentWeek ) ;
77- startDate . setDate ( mondayOfCurrentWeek . getDate ( ) + firstDayOffset ) ;
78- startDate . setHours ( 0 , 0 , 0 , 0 ) ;
79-
80- const endDate = new Date ( mondayOfCurrentWeek ) ;
81- endDate . setDate ( mondayOfCurrentWeek . getDate ( ) + lastDayOffset ) ;
82- endDate . setHours ( 23 , 59 , 0 , 0 ) ;
83-
84- const startDateString = formatDateTime ( startDate , true ) ;
85- const endDateString = formatDateTime ( endDate , true ) ;
86-
87- await prisma . assignedTask . create ( {
88- data : {
89- participant_id : participant . participant_id ,
90- task_name : task . task_name ,
91- task_type : task . task_type ,
92- start_date : startDateString ,
93- end_date : endDateString ,
94- marillac_bucks_addition : task . marillac_bucks_addition ,
95- marillac_bucks_deduction : task . marillac_bucks_deduction ,
96- comment : task . comment ,
97- } ,
98- } ) ;
99- }
100- }
101- }
103+ const startDateString = formatDateTime ( startDate , true ) ;
104+ const endDateString = formatDateTime ( endDate , true ) ;
105+
106+ await prisma . assignedTask . create ( {
107+ data : {
108+ participant_id : participant . participant_id ,
109+ task_name : task . task_name ,
110+ task_type : task . task_type ,
111+ start_date : startDateString ,
112+ end_date : endDateString ,
113+ marillac_bucks_addition : task . marillac_bucks_addition ,
114+ marillac_bucks_deduction : task . marillac_bucks_deduction ,
115+ comment : task . comment ,
116+ } ,
117+ } ) ;
118+ }
119+ } )
120+ ) ;
121+ } )
122+ ) ;
102123 return true ;
103124 } catch ( err ) {
104125 console . error ( err ) ;
105126 return false ;
106127 }
107- } ;
128+ }
108129
109130export default createAssignedTasks ;
0 commit comments