Merge pull request 'Fix some issues' (#4) from ajanin into cicd
All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 3m53s
All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 3m53s
Reviewed-on: #4
This commit is contained in:
commit
ed4b797230
75
src/common/Api/LeCoffreApi/sdk/AbstractService.ts
Normal file
75
src/common/Api/LeCoffreApi/sdk/AbstractService.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import MessageBus from 'src/sdk/MessageBus';
|
||||
|
||||
export default abstract class AbstractService {
|
||||
|
||||
protected static readonly messageBus: MessageBus = MessageBus.getInstance();
|
||||
|
||||
private static readonly CACHE_TTL = 45 * 60 * 1000; // 45 minutes cache TTL
|
||||
|
||||
protected constructor() { }
|
||||
|
||||
protected static setItem(key: string, process: any): void {
|
||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
|
||||
const index: number = list.findIndex((item: any) => item.process.processData.uid === process.processData.uid);
|
||||
if (index !== -1) {
|
||||
list[index] = {
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
} else {
|
||||
list.push({
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(list));
|
||||
}
|
||||
|
||||
protected static getItem(key: string, uid: string): any {
|
||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
if (list.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const item: any = list.find((item: any) => item.process.processData.uid === uid);
|
||||
if (!item) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
if ((now - item.timestamp) < this.CACHE_TTL) {
|
||||
return item.process;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static getItems(key: string): any[] {
|
||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const now = Date.now();
|
||||
|
||||
const items: any[] = [];
|
||||
for (const item of list) {
|
||||
if (now - item.timestamp < this.CACHE_TTL) {
|
||||
items.push(item.process);
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
protected static removeItem(key: string, uid: string): void {
|
||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
|
||||
const index: number = list.findIndex((item: any) => item.process.processData.uid === uid);
|
||||
if (index !== -1) {
|
||||
list.splice(index, 1);
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(list));
|
||||
}
|
||||
}
|
@ -1,15 +1,14 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import MessageBus from 'src/sdk/MessageBus';
|
||||
import User from 'src/sdk/User';
|
||||
|
||||
export default class CustomerService {
|
||||
import AbstractService from './AbstractService';
|
||||
|
||||
private static readonly messageBus: MessageBus = MessageBus.getInstance();
|
||||
export default class CustomerService extends AbstractService {
|
||||
|
||||
private static readonly CACHE_TTL = 45 * 60 * 1000; // 45 minutes cache TTL
|
||||
|
||||
private constructor() { }
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static createCustomer(customerData: any, validatorId: string): Promise<any> {
|
||||
const ownerId = User.getInstance().getPairingId()!;
|
||||
@ -81,15 +80,7 @@ export default class CustomerService {
|
||||
|
||||
public static getCustomers(): Promise<any[]> {
|
||||
// Check if we have valid cache
|
||||
const cacheProcesses: any[] = [];
|
||||
const now = Date.now();
|
||||
const customers: any[] = JSON.parse(sessionStorage.getItem('_customers_') || '[]');
|
||||
for (const customer of customers) {
|
||||
if (now - customer.timestamp < this.CACHE_TTL) {
|
||||
cacheProcesses.push(customer.process);
|
||||
}
|
||||
}
|
||||
const cacheUids: string[] = cacheProcesses.map((process: any) => process.processData.uid);
|
||||
const items: any[] = this.getItems('_customers_');
|
||||
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) =>
|
||||
publicValues['uid'] &&
|
||||
@ -97,28 +88,27 @@ export default class CustomerService {
|
||||
publicValues['utype'] === 'customer' &&
|
||||
publicValues['isDeleted'] &&
|
||||
publicValues['isDeleted'] === 'false' &&
|
||||
!cacheUids.includes(publicValues['uid'])
|
||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
||||
).then((processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
return cacheProcesses;
|
||||
return items;
|
||||
} else {
|
||||
for (const process of processes) {
|
||||
// Update cache
|
||||
this.setCache(process);
|
||||
this.setItem('_customers_', process);
|
||||
|
||||
cacheProcesses.push(process);
|
||||
items.push(process);
|
||||
}
|
||||
return cacheProcesses;
|
||||
return items;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getCustomerByUid(uid: string): Promise<any> {
|
||||
// Check if we have valid cache
|
||||
const now = Date.now();
|
||||
const cache: any = this.getCache(uid);
|
||||
if (cache && (now - cache.timestamp) < this.CACHE_TTL) {
|
||||
return Promise.resolve(cache.process);
|
||||
const item: any = this.getItem('_customers_', uid);
|
||||
if (item) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve: (process: any) => void, reject: (error: string) => void) => {
|
||||
@ -136,7 +126,7 @@ export default class CustomerService {
|
||||
const process: any = processes[0];
|
||||
|
||||
// Update cache
|
||||
this.setCache(process);
|
||||
this.setItem('_customers_', process);
|
||||
|
||||
resolve(process);
|
||||
}
|
||||
@ -151,7 +141,7 @@ export default class CustomerService {
|
||||
this.messageBus.notifyUpdate(process.processId, newStateId).then(() => {
|
||||
this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => {
|
||||
const customerUid: string = process.processData.uid;
|
||||
this.removeCache(customerUid);
|
||||
this.removeItem('_customers_', customerUid);
|
||||
|
||||
this.getCustomerByUid(customerUid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
@ -159,47 +149,4 @@ export default class CustomerService {
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
private static setCache(process: any): void {
|
||||
const key: string = '_customers_';
|
||||
|
||||
const customers: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const index: number = customers.findIndex((customer: any) => customer.process.processData.uid === process.processData.uid);
|
||||
if (index !== -1) {
|
||||
customers[index] = {
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
} else {
|
||||
customers.push({
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(customers));
|
||||
}
|
||||
|
||||
private static getCache(uid: string): any {
|
||||
const key: string = '_customers_';
|
||||
|
||||
const customers: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
if (customers.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return customers.find((customer: any) => customer.process.processData.uid === uid);
|
||||
}
|
||||
|
||||
private static removeCache(uid: string): void {
|
||||
const key: string = '_customers_';
|
||||
|
||||
const customers: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const index: number = customers.findIndex((customer: any) => customer.process.processData.uid === uid);
|
||||
if (index !== -1) {
|
||||
customers.splice(index, 1);
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(customers));
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import MessageBus from 'src/sdk/MessageBus';
|
||||
import User from 'src/sdk/User';
|
||||
|
||||
import AbstractService from './AbstractService';
|
||||
import DocumentTypeService from './DocumentTypeService';
|
||||
|
||||
export default class DeedTypeService {
|
||||
export default class DeedTypeService extends AbstractService {
|
||||
|
||||
private static readonly messageBus: MessageBus = MessageBus.getInstance();
|
||||
|
||||
private constructor() { }
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static createDeedType(deedTypeData: any, validatorId: string): Promise<any> {
|
||||
const ownerId = User.getInstance().getPairingId()!;
|
||||
@ -72,7 +72,7 @@ export default class DeedTypeService {
|
||||
this.messageBus.createProcess(processData, privateFields, roles).then((processCreated: any) => {
|
||||
this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id).then(() => {
|
||||
this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id).then((_stateValidated: any) => {
|
||||
resolve(processCreated);
|
||||
this.getDeedTypeByUid(processCreated.processData.uid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
@ -80,29 +80,49 @@ export default class DeedTypeService {
|
||||
}
|
||||
|
||||
public static getDeedTypes(): Promise<any[]> {
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['utype'] && publicValues['utype'] === 'deedType' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false');
|
||||
// Check if we have valid cache
|
||||
const items: any[] = this.getItems('_deed_types_');
|
||||
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) =>
|
||||
publicValues['uid'] &&
|
||||
publicValues['utype'] &&
|
||||
publicValues['utype'] === 'deedType' &&
|
||||
publicValues['isDeleted'] && publicValues['isDeleted'] === 'false' &&
|
||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
||||
).then(async (processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
return items;
|
||||
} else {
|
||||
for (let process of processes) {
|
||||
process = await this.completeDeedType(process);
|
||||
|
||||
// Update cache
|
||||
this.setItem('_deed_types_', process);
|
||||
|
||||
items.push(process);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getDeedTypeByUid(uid: string, includeDocumentTypes: boolean = true): Promise<any> {
|
||||
// Check if we have valid cache
|
||||
const item: any = this.getItem('_deed_types_', uid);
|
||||
if (item) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve: (process: any) => void, reject: (error: string) => void) => {
|
||||
this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['uid'] === uid && publicValues['utype'] && publicValues['utype'] === 'deedType' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false').then(async (processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
resolve(null);
|
||||
} else {
|
||||
const process: any = processes[0];
|
||||
let process: any = processes[0];
|
||||
process = await this.completeDeedType(process);
|
||||
|
||||
if (includeDocumentTypes && process.processData.document_types && process.processData.document_types.length > 0) {
|
||||
process.processData.document_types = await new Promise<any[]>(async (resolve: (document_types: any[]) => void) => {
|
||||
let document_types: any[] = [];
|
||||
for (const document_type of process.processData.document_types) {
|
||||
const p: any = await DocumentTypeService.getDocumentTypeByUid(document_type.uid);
|
||||
document_types.push(p.processData);
|
||||
}
|
||||
// Remove duplicates
|
||||
document_types = document_types.filter((item: any, index: number) => document_types.findIndex((t: any) => t.uid === item.uid) === index);
|
||||
resolve(document_types);
|
||||
});
|
||||
}
|
||||
// Update cache
|
||||
this.setItem('_deed_types_', process);
|
||||
|
||||
resolve(process);
|
||||
}
|
||||
@ -116,10 +136,30 @@ export default class DeedTypeService {
|
||||
const newStateId: string = processUpdated.diffs[0]?.state_id;
|
||||
this.messageBus.notifyUpdate(process.processId, newStateId).then(() => {
|
||||
this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => {
|
||||
resolve();
|
||||
const deedTypeUid: string = process.processData.uid;
|
||||
this.removeItem('_deed_types_', deedTypeUid);
|
||||
|
||||
this.getDeedTypeByUid(deedTypeUid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
private static async completeDeedType(process: any): Promise<any> {
|
||||
if (process.processData.document_types && process.processData.document_types.length > 0) {
|
||||
process.processData.document_types = await new Promise<any[]>(async (resolve: (document_types: any[]) => void) => {
|
||||
let document_types: any[] = [];
|
||||
for (const document_type of process.processData.document_types) {
|
||||
document_types.push((await DocumentTypeService.getDocumentTypeByUid(document_type.uid)).processData);
|
||||
}
|
||||
|
||||
// Remove duplicates
|
||||
document_types = document_types.filter((item: any, index: number) => document_types.findIndex((t: any) => t.uid === item.uid) === index);
|
||||
resolve(document_types);
|
||||
});
|
||||
}
|
||||
|
||||
return process
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import MessageBus from 'src/sdk/MessageBus';
|
||||
import User from 'src/sdk/User';
|
||||
|
||||
export default class DocumentService {
|
||||
import AbstractService from './AbstractService';
|
||||
|
||||
private static readonly messageBus: MessageBus = MessageBus.getInstance();
|
||||
export default class DocumentService extends AbstractService {
|
||||
|
||||
private static readonly CACHE_TTL = 45 * 60 * 1000; // 45 minutes cache TTL
|
||||
|
||||
private constructor() { }
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static createDocument(documentData: any, validatorId: string): Promise<any> {
|
||||
const ownerId = User.getInstance().getPairingId()!;
|
||||
@ -81,15 +80,7 @@ export default class DocumentService {
|
||||
|
||||
public static getDocuments(): Promise<any[]> {
|
||||
// Check if we have valid cache
|
||||
const cacheProcesses: any[] = [];
|
||||
const now = Date.now();
|
||||
const customers: any[] = JSON.parse(sessionStorage.getItem('_documents') || '[]');
|
||||
for (const customer of customers) {
|
||||
if (now - customer.timestamp < this.CACHE_TTL) {
|
||||
cacheProcesses.push(customer.process);
|
||||
}
|
||||
}
|
||||
const cacheUids: string[] = cacheProcesses.map((process: any) => process.processData.uid);
|
||||
const items: any[] = this.getItems('_documents_');
|
||||
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) =>
|
||||
publicValues['uid'] &&
|
||||
@ -97,28 +88,27 @@ export default class DocumentService {
|
||||
publicValues['utype'] === 'document' &&
|
||||
publicValues['isDeleted'] &&
|
||||
publicValues['isDeleted'] === 'false' &&
|
||||
!cacheUids.includes(publicValues['uid'])
|
||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
||||
).then((processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
return cacheProcesses;
|
||||
return items;
|
||||
} else {
|
||||
for (const process of processes) {
|
||||
// Update cache
|
||||
this.setCache(process);
|
||||
this.setItem('_documents_', process);
|
||||
|
||||
cacheProcesses.push(process);
|
||||
items.push(process);
|
||||
}
|
||||
return cacheProcesses;
|
||||
return items;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getDocumentByUid(uid: string): Promise<any> {
|
||||
// Check if we have valid cache
|
||||
const now = Date.now();
|
||||
const cache: any = this.getCache(uid);
|
||||
if (cache && (now - cache.timestamp) < this.CACHE_TTL) {
|
||||
return Promise.resolve(cache.process);
|
||||
const item: any = this.getItem('_documents_', uid);
|
||||
if (item) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve: (process: any) => void, reject: (error: string) => void) => {
|
||||
@ -136,7 +126,7 @@ export default class DocumentService {
|
||||
const process: any = processes[0];
|
||||
|
||||
// Update cache
|
||||
this.setCache(process);
|
||||
this.setItem('_documents_', process);
|
||||
|
||||
resolve(process);
|
||||
}
|
||||
@ -151,7 +141,7 @@ export default class DocumentService {
|
||||
this.messageBus.notifyUpdate(process.processId, newStateId).then(() => {
|
||||
this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => {
|
||||
const documentUid: string = process.processData.uid;
|
||||
this.removeCache(documentUid);
|
||||
this.removeItem('_documents_', documentUid);
|
||||
|
||||
this.getDocumentByUid(documentUid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
@ -159,47 +149,4 @@ export default class DocumentService {
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
private static setCache(process: any): void {
|
||||
const key: string = '_documents_';
|
||||
|
||||
const documents: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const index: number = documents.findIndex((document: any) => document.process.processData.uid === process.processData.uid);
|
||||
if (index !== -1) {
|
||||
documents[index] = {
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
} else {
|
||||
documents.push({
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(documents));
|
||||
}
|
||||
|
||||
private static getCache(uid: string): any {
|
||||
const key: string = '_documents_';
|
||||
|
||||
const documents: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
if (documents.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return documents.find((document: any) => document.process.processData.uid === uid);
|
||||
}
|
||||
|
||||
private static removeCache(uid: string): void {
|
||||
const key: string = '_documents_';
|
||||
|
||||
const documents: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const index: number = documents.findIndex((document: any) => document.process.processData.uid === uid);
|
||||
if (index !== -1) {
|
||||
documents.splice(index, 1);
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(documents));
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import MessageBus from 'src/sdk/MessageBus';
|
||||
import User from 'src/sdk/User';
|
||||
|
||||
export default class DocumentTypeService {
|
||||
import AbstractService from './AbstractService';
|
||||
|
||||
private static readonly messageBus: MessageBus = MessageBus.getInstance();
|
||||
export default class DocumentTypeService extends AbstractService {
|
||||
|
||||
private constructor() { }
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static createDocumentType(documentTypeData: any, validatorId: string): Promise<any> {
|
||||
const ownerId = User.getInstance().getPairingId()!;
|
||||
@ -70,7 +71,7 @@ export default class DocumentTypeService {
|
||||
this.messageBus.createProcess(processData, privateFields, roles).then((processCreated: any) => {
|
||||
this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id).then(() => {
|
||||
this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id).then((_stateValidated: any) => {
|
||||
resolve(processCreated);
|
||||
this.getDocumentTypeByUid(processCreated.processData.uid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
@ -78,16 +79,48 @@ export default class DocumentTypeService {
|
||||
}
|
||||
|
||||
public static getDocumentTypes(): Promise<any[]> {
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['utype'] && publicValues['utype'] === 'documentType' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false');
|
||||
// Check if we have valid cache
|
||||
const items: any[] = this.getItems('_document_types_');
|
||||
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) =>
|
||||
publicValues['uid'] &&
|
||||
publicValues['utype'] &&
|
||||
publicValues['utype'] === 'documentType' &&
|
||||
publicValues['isDeleted'] &&
|
||||
publicValues['isDeleted'] === 'false' &&
|
||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
||||
).then(async (processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
return items;
|
||||
} else {
|
||||
for (const process of processes) {
|
||||
// Update cache
|
||||
this.setItem('_document_types_', process);
|
||||
|
||||
items.push(process);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getDocumentTypeByUid(uid: string): Promise<any> {
|
||||
// Check if we have valid cache
|
||||
const item: any = this.getItem('_document_types_', uid);
|
||||
if (item) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve: (process: any) => void, reject: (error: string) => void) => {
|
||||
this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['uid'] === uid && publicValues['utype'] && publicValues['utype'] === 'documentType' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false').then((processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
resolve(null);
|
||||
} else {
|
||||
const process: any = processes[0];
|
||||
|
||||
// Update cache
|
||||
this.setItem('_document_types_', process);
|
||||
|
||||
resolve(process);
|
||||
}
|
||||
}).catch(reject);
|
||||
@ -100,7 +133,10 @@ export default class DocumentTypeService {
|
||||
const newStateId: string = processUpdated.diffs[0]?.state_id;
|
||||
this.messageBus.notifyUpdate(process.processId, newStateId).then(() => {
|
||||
this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => {
|
||||
resolve();
|
||||
const documentTypeUid: string = process.processData.uid;
|
||||
this.removeItem('_document_types_', documentTypeUid);
|
||||
|
||||
this.getDocumentTypeByUid(documentTypeUid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
}).catch(reject);
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import MessageBus from 'src/sdk/MessageBus';
|
||||
import User from 'src/sdk/User';
|
||||
|
||||
import AbstractService from './AbstractService';
|
||||
import CustomerService from './CustomerService';
|
||||
import DeedTypeService from './DeedTypeService';
|
||||
import DocumentTypeService from './DocumentTypeService';
|
||||
@ -10,13 +10,15 @@ import DocumentService from './DocumentService';
|
||||
import FileService from './FileService';
|
||||
import NoteService from './NoteService';
|
||||
|
||||
export default class FolderService {
|
||||
export default class FolderService extends AbstractService {
|
||||
|
||||
private static readonly messageBus: MessageBus = MessageBus.getInstance();
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
private static readonly CACHE_TTL = 45 * 60 * 1000; // 45 minutes cache TTL
|
||||
|
||||
private constructor() { }
|
||||
public static invalidateCache(uid: string): void {
|
||||
this.removeItem('_folders_', uid);
|
||||
}
|
||||
|
||||
public static createFolder(folderData: any, stakeholdersId: string[], customersId: string[]): Promise<any> {
|
||||
const ownerId = User.getInstance().getPairingId()!;
|
||||
@ -94,15 +96,7 @@ export default class FolderService {
|
||||
|
||||
public static getFolders(): Promise<any[]> {
|
||||
// Check if we have valid cache
|
||||
const cacheProcesses: any[] = [];
|
||||
const now = Date.now();
|
||||
const folders: any[] = JSON.parse(sessionStorage.getItem('_folders_') || '[]');
|
||||
for (const folder of folders) {
|
||||
if (now - folder.timestamp < this.CACHE_TTL) {
|
||||
cacheProcesses.push(folder.process);
|
||||
}
|
||||
}
|
||||
const cacheUids: string[] = cacheProcesses.map((process: any) => process.processData.uid);
|
||||
const items: any[] = this.getItems('_folders_');
|
||||
|
||||
return this.messageBus.getProcessesDecoded((publicValues: any) =>
|
||||
publicValues['uid'] &&
|
||||
@ -110,30 +104,29 @@ export default class FolderService {
|
||||
publicValues['utype'] === 'folder' &&
|
||||
publicValues['isDeleted'] &&
|
||||
publicValues['isDeleted'] === 'false' &&
|
||||
!cacheUids.includes(publicValues['uid'])
|
||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
||||
).then(async (processes: any[]) => {
|
||||
if (processes.length === 0) {
|
||||
return cacheProcesses;
|
||||
return items;
|
||||
} else {
|
||||
for (const process of processes) {
|
||||
await this.completeFolder(process);
|
||||
for (let process of processes) {
|
||||
process = await this.completeFolder(process);
|
||||
|
||||
// Update cache
|
||||
this.setCache(process);
|
||||
this.setItem('_folders_', process);
|
||||
|
||||
cacheProcesses.push(process);
|
||||
items.push(process);
|
||||
}
|
||||
return cacheProcesses;
|
||||
return items;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getFolderByUid(uid: string): Promise<any> {
|
||||
// Check if we have valid cache
|
||||
const now = Date.now();
|
||||
const cache: any = this.getCache(uid);
|
||||
if (cache && (now - cache.timestamp) < this.CACHE_TTL) {
|
||||
return Promise.resolve(cache.process);
|
||||
const item: any = this.getItem('_folders_', uid);
|
||||
if (item) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve: (process: any) => void, reject: (error: string) => void) => {
|
||||
@ -148,11 +141,11 @@ export default class FolderService {
|
||||
if (processes.length === 0) {
|
||||
resolve(null);
|
||||
} else {
|
||||
const process: any = processes[0];
|
||||
await this.completeFolder(process);
|
||||
let process: any = processes[0];
|
||||
process = await this.completeFolder(process);
|
||||
|
||||
// Update cache
|
||||
this.setCache(process);
|
||||
this.setItem('_folders_', process);
|
||||
|
||||
resolve(process);
|
||||
}
|
||||
@ -167,7 +160,7 @@ export default class FolderService {
|
||||
this.messageBus.notifyUpdate(process.processId, newStateId).then(() => {
|
||||
this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => {
|
||||
const folderUid: string = process.processData.uid;
|
||||
this.removeCache(folderUid);
|
||||
this.removeItem('_folders_', folderUid);
|
||||
|
||||
this.getFolderByUid(folderUid).then(resolve).catch(reject);
|
||||
}).catch(reject);
|
||||
@ -181,8 +174,7 @@ export default class FolderService {
|
||||
process.processData.customers = await new Promise<any[]>(async (resolve: (customers: any[]) => void) => {
|
||||
const customers: any[] = [];
|
||||
for (const customer of process.processData.customers) {
|
||||
const p: any = await CustomerService.getCustomerByUid(customer.uid);
|
||||
customers.push(p.processData);
|
||||
customers.push((await CustomerService.getCustomerByUid(customer.uid)).processData);
|
||||
}
|
||||
resolve(customers);
|
||||
});
|
||||
@ -200,7 +192,8 @@ export default class FolderService {
|
||||
if (document.files && document.files.length > 0) {
|
||||
const files: any[] = [];
|
||||
for (const file of document.files) {
|
||||
files.push((await FileService.getFileByUid(file.uid)).processData);
|
||||
const p: any = await FileService.getFileByUid(file.uid);
|
||||
files.push({ uid: p.processData.uid, file_name: p.processData.file_name });
|
||||
}
|
||||
document.files = files;
|
||||
}
|
||||
@ -210,12 +203,12 @@ export default class FolderService {
|
||||
}
|
||||
|
||||
if (process.processData.deed && process.processData.deed.deed_type) {
|
||||
const p: any = await DeedTypeService.getDeedTypeByUid(process.processData.deed.deed_type.uid);
|
||||
process.processData.deed.deed_type = p.processData;
|
||||
const deed_type: any = (await DeedTypeService.getDeedTypeByUid(process.processData.deed.deed_type.uid)).processData;
|
||||
process.processData.deed.deed_type = deed_type;
|
||||
|
||||
// Remove duplicates - TODO: review - see getDeedTypeByUid - completeDeedType
|
||||
if (p.processData.document_types && p.processData.document_types.length > 0) {
|
||||
process.processData.deed.document_types = p.processData.document_types.filter((item: any, index: number) => p.processData.document_types.findIndex((t: any) => t.uid === item.uid) === index);
|
||||
if (deed_type.document_types && deed_type.document_types.length > 0) {
|
||||
// Remove duplicates
|
||||
process.processData.deed.document_types = deed_type.document_types.filter((item: any, index: number) => deed_type.document_types.findIndex((t: any) => t.uid === item.uid) === index);
|
||||
}
|
||||
}
|
||||
|
||||
@ -226,47 +219,4 @@ export default class FolderService {
|
||||
|
||||
return process;
|
||||
}
|
||||
|
||||
private static setCache(process: any): void {
|
||||
const key: string = '_folders_';
|
||||
|
||||
const folders: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const index: number = folders.findIndex((folder: any) => folder.process.processData.uid === process.processData.uid);
|
||||
if (index !== -1) {
|
||||
folders[index] = {
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
} else {
|
||||
folders.push({
|
||||
process: process,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(folders));
|
||||
}
|
||||
|
||||
private static getCache(uid: string): any {
|
||||
const key: string = '_folders_';
|
||||
|
||||
const folders: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
if (folders.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return folders.find((folder: any) => folder.process.processData.uid === uid);
|
||||
}
|
||||
|
||||
private static removeCache(uid: string): void {
|
||||
const key: string = '_folders_';
|
||||
|
||||
const folders: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
||||
const index: number = folders.findIndex((folder: any) => folder.process.processData.uid === uid);
|
||||
if (index !== -1) {
|
||||
folders.splice(index, 1);
|
||||
}
|
||||
|
||||
sessionStorage.setItem(key, JSON.stringify(folders));
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,10 @@ import { EDocumentStatus } from "le-coffre-resources/dist/Customer/Document";
|
||||
import { ToasterService } from "@Front/Components/DesignSystem/Toaster";
|
||||
import Confirm from "@Front/Components/DesignSystem/OldModal/Confirm";
|
||||
|
||||
import FileService from "src/common/Api/LeCoffreApi/sdk/FileService";
|
||||
import FolderService from "src/common/Api/LeCoffreApi/sdk/FolderService";
|
||||
import DocumentService from "src/common/Api/LeCoffreApi/sdk/DocumentService";
|
||||
import FileService from "src/common/Api/LeCoffreApi/sdk/FileService";
|
||||
|
||||
import { FileBlob, FileData } from "@Front/Api/Entities/types";
|
||||
import WatermarkService from "@Front/Services/WatermarkService";
|
||||
|
||||
@ -68,6 +70,7 @@ export default function DepositDocumentComponent(props: IProps) {
|
||||
DocumentService.getDocumentByUid(document.uid!).then((process: any) => {
|
||||
if (process) {
|
||||
const document: any = process.processData;
|
||||
FolderService.invalidateCache(document.folder.uid);
|
||||
|
||||
let files: any[] = document.files;
|
||||
if (!files) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user