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

163 lines
3.0 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { AES, enc, SHA1 } from 'crypto-js'
2023-02-03 19:37:47 +01:00
import { isArray } from 'ionic-angular/umd/util/util';
2023-04-20 11:45:46 +01:00
import { EventoApprovePipe } from 'src/app/pipes/evento-approve.pipe'
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 EventoaprovacaoStoreService {
// main data
2023-02-22 10:08:50 +01:00
private _list: {[key: string]: any[]} = {}
2023-02-22 13:06:31 +01:00
private _concatList = []
2023-02-22 10:08:50 +01:00
// local storage keyName
2023-02-22 10:08:50 +01:00
private keyNameAll: string;
private _counts : {[key: string]: number} = {}
private _countsAll = 0
2023-04-17 16:00:30 +01:00
2023-04-20 11:45:46 +01:00
EventoApprovePipe = new EventoApprovePipe()
2023-04-19 12:34:28 +01:00
callbacks: {
[key: string]: {
funx: Function
id: string
}
} = {}
2023-04-17 16:00:30 +01:00
newList = []
2021-07-23 15:57:01 +01:00
constructor() {
2023-02-22 10:08:50 +01:00
this.keyNameAll = (SHA1("EventoaprovacaoStoreService"+"all")).toString()
2021-07-23 15:57:01 +01:00
2023-03-16 10:45:05 +01:00
setTimeout(() => {
2023-02-22 13:06:31 +01:00
let {list , counts} = localstoreService.get(this.keyNameAll, {})
2023-02-22 13:06:31 +01:00
this._list = list || {}
2023-02-22 13:06:31 +01:00
this.updateCount();
2023-05-04 09:48:04 +01:00
this.runCallback()
2023-02-22 13:06:31 +01:00
}, 10)
2021-07-23 15:57:01 +01:00
}
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
}
runCallback() {
for (const [key, value] of Object.entries(this.callbacks)) {
value.funx()
}
}
2023-02-22 13:06:31 +01:00
get countAll() {
return this._countsAll
2021-07-23 15:57:01 +01:00
}
2023-02-22 13:06:31 +01:00
get contactList() {
return this._concatList
2021-07-23 15:57:01 +01:00
}
2023-02-22 10:08:50 +01:00
2023-04-20 11:45:46 +01:00
save(segment: string, value: any[]) {
2023-03-16 10:45:05 +01:00
if(typeof segment == 'object') {
throw("segment most be userId")
}
2023-02-22 10:08:50 +01:00
if(!this._list[segment]) {
this._list[segment] = []
this._counts[segment] = 0
}
2023-05-04 09:48:04 +01:00
2023-02-22 10:08:50 +01:00
2023-04-20 11:45:46 +01:00
value = value.map( e => this.EventoApprovePipe.transform(e, e))
2023-02-22 10:08:50 +01:00
this._list[segment] = value
this._counts[segment] = value.length
2023-07-16 21:38:37 +01:00
this.runCallback()
2023-02-22 10:08:50 +01:00
this.updateCount();
setTimeout(() => {
localstoreService.set(this.keyNameAll,{
2023-02-22 13:06:31 +01:00
list: this._list,
counts: this._counts
})
2023-05-04 09:48:04 +01:00
2023-02-22 13:06:31 +01:00
}, 10)
if(window['all-process-gabinete']) {
window['all-process-gabinete']()
}
}
clear() {
this._list = {}
this._counts = {}
setTimeout(() => {
localstoreService.set(this.keyNameAll,{
list: this._list,
counts: this._counts
2023-02-22 10:08:50 +01:00
})
}, 10)
}
get(segment) {
return this._list[segment] || []
}
updateCount() {
2023-02-22 13:06:31 +01:00
let allList = []
2023-02-22 10:08:50 +01:00
for(let [name, value] of Object.entries(this._list)) {
2023-02-22 13:06:31 +01:00
allList = allList.concat(value)
2023-02-22 10:08:50 +01:00
}
2023-02-22 13:06:31 +01:00
this._countsAll = allList.length
this._concatList = allList
2023-04-17 16:00:30 +01:00
this.newList = this._concatList.filter((e) =>{
return this.lessthen24Hours(e.TaskStartDate || e.taskStartDate)
})
2023-02-22 10:08:50 +01:00
}
2023-02-22 13:06:31 +01:00
2023-04-17 16:00:30 +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-17 16:00:30 +01:00
}
}
export const EventoAprovacaoStore = new EventoaprovacaoStoreService()