Files
doneit-web/src/app/store/pedidos-store.service.ts
T

167 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { AES, enc, SHA1 } from 'crypto-js'
2023-04-28 12:56:45 +01:00
import { momentG } from 'src/plugin/momentG';
2023-06-11 20:17:10 +01:00
import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
})
export class PedidosStoreService {
2023-04-13 12:51:38 +01:00
private _listParecer = []
private _listDeferimento = []
// local storage keyName
private keyNameparecer: string;
private keyNamedeferiemnto: string;
2021-07-23 05:43:52 +01:00
private _countparecer = 0
2021-07-30 16:55:13 +01:00
private _countDeferiemnto = 0
2023-04-13 12:51:38 +01:00
listParecerCount: Event[] = []
listDeferimentoCount: Event[] = []
2023-04-19 12:34:28 +01:00
callbacks: {
[key: string]: {
funx: Function
id: string
}
} = {}
constructor() {
2023-01-12 15:27:09 +01:00
this.keyNameparecer = (SHA1("PedidosStoreService"+"parecer")).toString()
this.keyNamedeferiemnto = (SHA1("PedidosStoreService"+"deferimneto")).toString()
setTimeout(()=>{
2021-07-30 16:55:13 +01:00
let restoreParecer = localstoreService.get(this.keyNameparecer, {})
let restoreDeferimento = localstoreService.get(this.keyNamedeferiemnto, {})
2021-07-23 16:05:43 +01:00
2021-07-30 16:55:13 +01:00
this._listParecer = restoreDeferimento.listDeferimento || []
this._listDeferimento = restoreParecer.listParecer || []
this._countparecer = parseInt(restoreParecer.count) || 0
this._countDeferiemnto = parseInt(restoreDeferimento.count) || 0
2023-05-04 09:48:04 +01:00
this.runCallback()
2021-07-23 16:05:43 +01:00
}, 10)
}
2023-04-19 12:34:28 +01:00
2023-06-11 20:17:10 +01:00
registerCallback({funx, id = uuidv4()}) {
2023-04-19 12:34:28 +01:00
this.callbacks[id] = { funx, id}
2023-06-11 20:17:10 +01:00
return {
delete: ()=> {
delete this.callbacks[id]
}
}
2023-04-19 12:34:28 +01:00
}
2023-06-11 20:17:10 +01:00
2023-04-19 12:34:28 +01:00
runCallback() {
for (const [key, value] of Object.entries(this.callbacks)) {
value.funx()
}
}
2022-05-23 14:54:40 +01:00
get listparecer(): any[] {
return this._listParecer || []
}
2022-05-23 14:54:40 +01:00
get listdeferimento(): any[] {
return this._listDeferimento || []
2021-07-30 16:55:13 +01:00
}
2021-07-23 05:43:52 +01:00
get countparecer() {
2021-08-25 08:51:52 +01:00
return this._countparecer || 0
}
2021-07-23 10:35:53 +01:00
set countparecer(value: number) {
2021-07-23 05:43:52 +01:00
this._countparecer = value
2021-07-30 16:55:13 +01:00
this.saveParecer()
2021-07-23 05:43:52 +01:00
}
get countdeferimento() {
2021-08-25 08:51:52 +01:00
return this._countDeferiemnto || 0
2021-07-23 05:43:52 +01:00
}
2021-07-23 10:35:53 +01:00
set countdeferimento(value: number ) {
2021-07-30 16:55:13 +01:00
this._countDeferiemnto = value
this.saveDeferimento()
}
resetparecer(eventsList: any) {
2021-07-30 16:55:13 +01:00
this._listParecer = eventsList
2021-07-30 16:55:13 +01:00
this.countparecer = this._listParecer.length
this.saveDeferimento()
2023-05-04 09:48:04 +01:00
this.runCallback()
2023-02-13 17:51:36 +01:00
if(window['all-process-gabinete']) {
window['all-process-gabinete']()
}
}
2021-07-30 16:55:13 +01:00
resetdeferimento(list: any) {
this._listDeferimento = list
2021-07-30 16:55:13 +01:00
this.countdeferimento =this._listDeferimento.length
this.saveParecer()
2023-05-04 09:48:04 +01:00
this.runCallback()
2023-02-13 17:51:36 +01:00
if(window['all-process-gabinete']) {
window['all-process-gabinete']()
}
2021-07-30 16:55:13 +01:00
}
private saveParecer() {
setTimeout(()=>{
localstoreService.set(this.keyNameparecer,{
2021-07-30 16:55:13 +01:00
listParecer: this._listParecer,
count: this._countparecer,
})
}, 10)
2023-04-13 12:51:38 +01:00
this.updateNewCount()
}
2021-07-30 16:55:13 +01:00
private saveDeferimento() {
setTimeout(()=>{
localstoreService.set(this.keyNamedeferiemnto,{
listDeferimento: this._listParecer,
count: this._countDeferiemnto,
})
}, 10)
2023-04-13 12:51:38 +01:00
this.updateNewCount()
2021-07-30 16:55:13 +01:00
}
2023-04-13 12:51:38 +01:00
updateNewCount() {
this.listParecerCount = this._listParecer.filter((e) =>{
2023-04-19 09:18:25 +01:00
return this.lessthen24Hours(e.TaskReceiveDate)
2023-04-13 12:51:38 +01:00
})
this.listDeferimentoCount = this._listDeferimento.filter((e) =>{
2023-04-19 09:18:25 +01:00
return this.lessthen24Hours(e.TaskReceiveDate)
2023-04-13 12:51:38 +01:00
})
}
lessthen24Hours(isoDateString:string) {
2023-04-28 12:56:45 +01:00
if(!isoDateString) {
return false
}
const creationDate = new Date(isoDateString)
return momentG(new Date(), 'dd MMMM yyyy') == momentG(new Date(creationDate), 'dd MMMM yyyy')
2023-04-13 12:51:38 +01:00
}
}
export const PedidosStore = new PedidosStoreService()