@@ -11,13 +11,19 @@ const prisma = new PrismaClient();
1111
1212export const createAlerts = async ( ) : Promise < void > => {
1313 try {
14- const date = momentTimezone . tz ( 'America/Sao_Paulo' ) ;
14+ const dateA = momentTimezone . utc ( ) ;
1515
16- if ( date . hours ( ) < 12 ) {
17- await prisma . fence_tracker_triggers . deleteMany ( { where : { updated_at : { lt : date . clone ( ) . hours ( 0 ) . minutes ( 0 ) . seconds ( 0 ) . toDate ( ) } } } ) ;
18- } else {
19- await prisma . fence_tracker_triggers . deleteMany ( { where : { updated_at : { lt : date . clone ( ) . hours ( 12 ) . minutes ( 0 ) . seconds ( 0 ) . toDate ( ) } } } ) ;
20- }
16+ await prisma . fence_tracker_triggers . deleteMany (
17+ {
18+ where : {
19+ updated_at : {
20+ lt : dateA . hours ( ) < 15
21+ ? dateA . clone ( ) . hours ( 3 ) . minutes ( 0 ) . seconds ( 0 ) . milliseconds ( 0 ) . toDate ( )
22+ : dateA . clone ( ) . hours ( 15 ) . minutes ( 0 ) . seconds ( 0 ) . milliseconds ( 0 ) . toDate ( )
23+ }
24+ }
25+ }
26+ ) ;
2127
2228 const queryGatewayHttpClientInstance = new HttpClientUtil . HttpClient ( ) ;
2329
@@ -42,24 +48,18 @@ export const createAlerts = async (): Promise<void> => {
4248 )
4349 ) . data ?. data ?. fence_tracker_get_alert_map_list ;
4450
45- if ( ! alertMapListA ) {
51+ if ( ! alertMapListA ?. length ) {
4652 return ;
4753 }
4854
55+ const dateB = momentTimezone . tz ( 'America/Sao_Paulo' ) ;
56+ const startDate = dateB . hours ( ) < 12 ? dateB . clone ( ) . hours ( 0 ) . minutes ( 0 ) . seconds ( 0 ) : dateB . clone ( ) . hours ( 12 ) . minutes ( 0 ) . seconds ( 0 ) ;
57+ const endDate = dateB . hours ( ) < 12 ? dateB . clone ( ) . hours ( 11 ) . minutes ( 59 ) . seconds ( 59 ) : dateB . clone ( ) . hours ( 23 ) . minutes ( 59 ) . seconds ( 59 ) ;
4958 const whatsAppHttpClientInstance = new HttpClientUtil . HttpClient ( ) ;
59+ const startDateFormattation = startDate . clone ( ) . format ( 'DD/MM/YYYY HH:mm:ss' ) ;
60+ const endDateFormattation = endDate . clone ( ) . format ( 'DD/MM/YYYY HH:mm:ss' ) ;
5061
51- let startDateFormattation ;
52- let endDateFormattation ;
53-
54- if ( date . hours ( ) < 12 ) {
55- startDateFormattation = date . clone ( ) . hours ( 0 ) . minutes ( 0 ) . seconds ( 0 ) ;
56- endDateFormattation = date . clone ( ) . hours ( 11 ) . minutes ( 59 ) . seconds ( 59 ) ;
57- } else {
58- startDateFormattation = date . clone ( ) . hours ( 12 ) . minutes ( 0 ) . seconds ( 0 ) ;
59- endDateFormattation = date . clone ( ) . hours ( 23 ) . minutes ( 59 ) . seconds ( 59 ) ;
60- }
61-
62- await Promise . allSettled (
62+ Promise . allSettled (
6363 alertMapListA . map (
6464 async ( alertMap : IAlertMap . IAlertMap ) : Promise < void > => {
6565 const alertMapAccountCode = alertMap . account_code ;
@@ -78,8 +78,8 @@ export const createAlerts = async (): Promise<void> => {
7878 ) ;
7979
8080 const alertMapQuantityMultiple = Math . floor ( alertMapQuantity / EVENTS_COUNT_THRESHOLD ) * EVENTS_COUNT_THRESHOLD ;
81-
82- const processAlertMap = async ( ) : Promise < void > => {
81+
82+ if ( ! fenceTrackerTrigger && alertMapQuantity >= EVENTS_COUNT_THRESHOLD ) {
8383 await prisma . fence_tracker_triggers . create (
8484 {
8585 data : {
@@ -89,8 +89,22 @@ export const createAlerts = async (): Promise<void> => {
8989 }
9090 }
9191 ) ;
92-
93- await whatsAppHttpClientInstance . post < unknown > (
92+
93+ await prisma . fence_tracker_registers . create (
94+ {
95+ data : {
96+ account_code : alertMapAccountCode ,
97+ condominium : alertMap . condominium ,
98+ cabinet : alertMap . cabinet ,
99+ zone_name : alertMapZoneName ,
100+ quantity : alertMapQuantityMultiple ,
101+ period_started_at : startDate . toDate ( ) ,
102+ period_ended_at : endDate . toDate ( )
103+ }
104+ }
105+ ) ;
106+
107+ whatsAppHttpClientInstance . post < unknown > (
94108 `https://v5.chatpro.com.br/${ process . env . CHAT_PRO_INSTANCE_ID } /api/v1/send_message` ,
95109 {
96110 message : `⚠️ *ALERTA CERCA* ⚠️\n\n*Conta:* ${ alertMapAccountCode } \n*Condomínio:* ${ alertMap . condominium } \n*Armário:* ${ alertMap . cabinet } \n*Zona:* ${ alertMapZoneName } \n*Quantidate*: ${ alertMapQuantityMultiple } \n*Período Inicial:* ${ startDateFormattation } \n*Período Final:* ${ endDateFormattation } ` ,
@@ -101,12 +115,44 @@ export const createAlerts = async (): Promise<void> => {
101115 params : { instance_id : process . env . CHAT_PRO_INSTANCE_ID }
102116 }
103117 ) ;
104- } ;
105-
106- if ( ! fenceTrackerTrigger && alertMapQuantity >= EVENTS_COUNT_THRESHOLD ) {
107- processAlertMap ( ) ;
108118 } else if ( fenceTrackerTrigger && alertMapQuantityMultiple > fenceTrackerTrigger . quantity ) {
109- processAlertMap ( ) ;
119+ await prisma . fence_tracker_triggers . update (
120+ {
121+ where : {
122+ account_code_zone_name : {
123+ account_code : alertMapAccountCode ,
124+ zone_name : alertMapZoneName
125+ }
126+ } ,
127+ data : { quantity : alertMapQuantityMultiple }
128+ }
129+ ) ;
130+
131+ await prisma . fence_tracker_registers . create (
132+ {
133+ data : {
134+ account_code : alertMapAccountCode ,
135+ condominium : alertMap . condominium ,
136+ cabinet : alertMap . cabinet ,
137+ zone_name : alertMapZoneName ,
138+ quantity : alertMapQuantityMultiple ,
139+ period_started_at : startDate . toDate ( ) ,
140+ period_ended_at : endDate . toDate ( )
141+ }
142+ }
143+ ) ;
144+
145+ whatsAppHttpClientInstance . post < unknown > (
146+ `https://v5.chatpro.com.br/${ process . env . CHAT_PRO_INSTANCE_ID } /api/v1/send_message` ,
147+ {
148+ message : `⚠️ *ALERTA CERCA* ⚠️\n\n*Conta:* ${ alertMapAccountCode } \n*Condomínio:* ${ alertMap . condominium } \n*Armário:* ${ alertMap . cabinet } \n*Zona:* ${ alertMapZoneName } \n*Quantidate*: ${ alertMapQuantityMultiple } \n*Período Inicial:* ${ startDateFormattation } \n*Período Final:* ${ endDateFormattation } ` ,
149+ number : process . env . CHAT_PRO_NUMBER
150+ } ,
151+ {
152+ headers : { Authorization : process . env . CHAT_PRO_BEARER_TOKEN } ,
153+ params : { instance_id : process . env . CHAT_PRO_INSTANCE_ID }
154+ }
155+ ) ;
110156 }
111157 }
112158 )
0 commit comments