mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
270 lines
7.4 KiB
TypeScript
270 lines
7.4 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { PublicationFolder } from '../models/publicationfolder';
|
|
import { Storage } from '@ionic/storage';
|
|
import { Publication } from '../models/publication';
|
|
import { PublicationsService } from '../services/publications.service';
|
|
import { PublicationPipe } from 'src/app/pipes/publication.pipe';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class PublicationFolderService {
|
|
|
|
publicationList: { [key: string]: Publication[] } = {};
|
|
FolderDetails: { [key: string]: PublicationFolder } = {};
|
|
|
|
restoreFolder: {} = {}
|
|
|
|
keyName: string
|
|
|
|
showLoader = true
|
|
publicationPipe = new PublicationPipe()
|
|
getpublication = [];
|
|
|
|
gotoAction: string | false
|
|
|
|
|
|
constructor(
|
|
private storage: Storage,
|
|
private publications: PublicationsService,
|
|
) { }
|
|
|
|
clear() {
|
|
this.publicationList = {};
|
|
this.FolderDetails = {};
|
|
|
|
this.restoreFolder = {}
|
|
|
|
this.showLoader = true
|
|
this.publicationPipe = new PublicationPipe()
|
|
this.getpublication = [];
|
|
|
|
}
|
|
|
|
|
|
createPublicationList(folderId) {
|
|
if (!this.publicationList[folderId]) {
|
|
this.publicationList[folderId] = []
|
|
}
|
|
if (!this.FolderDetails[folderId]) {
|
|
this.FolderDetails[folderId] = new PublicationFolder();
|
|
}
|
|
}
|
|
|
|
getFromDB(folderId: any) {
|
|
|
|
|
|
if (!this.restoreFolder[folderId]) {
|
|
|
|
this.storage.get(folderId).then((viewPublications) => {
|
|
this.publicationList[folderId] = viewPublications
|
|
})
|
|
|
|
this.storage.get(folderId + "name").then((viewPublications) => {
|
|
this.FolderDetails[folderId] = viewPublications
|
|
})
|
|
}
|
|
|
|
this.restoreFolder[folderId] = true
|
|
}
|
|
|
|
updateFolderDetails(folderId, res) {
|
|
this.FolderDetails[folderId] = res
|
|
this.storage.set(folderId + "name", res)
|
|
}
|
|
|
|
save(folderId) {
|
|
this.storage.set(folderId + "name", this.FolderDetails)
|
|
this.storage.set(folderId, this.publicationList[folderId]);
|
|
}
|
|
|
|
publicationIsPresent(publicationId, folderId) {
|
|
return this.publicationList[folderId].find(e => e.DocumentId == publicationId)
|
|
}
|
|
publicationFind(publicationId, folderId) {
|
|
return this.publicationList[folderId].find(e => e.DocumentId == publicationId)
|
|
}
|
|
publicationFindIndex(publicationId, folderId) {
|
|
return this.publicationList[folderId].findIndex(e => e.DocumentId == publicationId)
|
|
}
|
|
|
|
PublicationAddOrUpdate(folderId, publicationId, Publication: Publication) {
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
if (!found) {
|
|
this.publicationList[folderId].push(Publication)
|
|
this.revertPublicationOrder(folderId);
|
|
} else {
|
|
this.publicationList[folderId][findIndex] = Publication
|
|
}
|
|
}
|
|
|
|
deletePost(folderId: any, DocumentId: any) {
|
|
|
|
if (this.publicationList[folderId]) {
|
|
|
|
this.publicationList[folderId] = this.publicationList[folderId].filter(e => e.DocumentId != DocumentId)
|
|
|
|
this.save(folderId)
|
|
}
|
|
}
|
|
|
|
|
|
getPublicationDetail(folderId) {
|
|
|
|
this.publications.GetPresidentialAction(folderId).subscribe(res => {
|
|
this.FolderDetails[folderId] = res
|
|
this.storage.set(folderId + "name", res)
|
|
}, (error) => {
|
|
this.showLoader = false;
|
|
// this.httpErroHandle.httpStatusHandle(error)
|
|
});
|
|
}
|
|
|
|
|
|
async getPublicationsIds(folderId) {
|
|
|
|
this.showLoader = true;
|
|
|
|
try {
|
|
const publicationIds = await this.publications.GetPublicationsList(folderId).toPromise();
|
|
|
|
this.createPublicationList(folderId)
|
|
let loadLater = []
|
|
for (let publicationId of publicationIds) {
|
|
|
|
if (!this.publicationIsPresent(publicationId, folderId)) {
|
|
await this.loadPublication(publicationId, folderId)
|
|
|
|
} else {
|
|
loadLater.push(publicationId)
|
|
}
|
|
}
|
|
|
|
for (let publicationId of loadLater) {
|
|
await this.loadPublication(publicationId, folderId)
|
|
}
|
|
|
|
for (let localPublication of this.publicationList[folderId]) {
|
|
|
|
const apiPublication = publicationIds.includes(localPublication.DocumentId)
|
|
if (!apiPublication) {
|
|
this.deletePost(folderId, localPublication.DocumentId)
|
|
}
|
|
}
|
|
|
|
|
|
this.showLoader = false;
|
|
|
|
this.storage.set(folderId, this.publicationList[folderId]);
|
|
this.getpublication = this.publicationList[folderId];
|
|
} catch (error) {
|
|
this.showLoader = false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
async loadPublication(publicationId, folderId) {
|
|
let Publication = await this.publications.GetPublicationWithArrayOfFilesById(publicationId).toPromise();
|
|
let publicationDetails: Publication = this.publicationPipe.itemList(Publication)
|
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
if (!found) {
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
this.revertPublicationOrder(folderId);
|
|
} else {
|
|
|
|
let a: any = Object.assign({}, this.publicationList[folderId][findIndex])
|
|
let b: any = Object.assign({}, publicationDetails)
|
|
|
|
a.Files = a.Files.length
|
|
b.Files = b.Files.length
|
|
|
|
if (JSON.stringify(a) != JSON.stringify(b)) {
|
|
|
|
console.log({a, b})
|
|
this.publicationList[folderId][findIndex] = publicationDetails
|
|
} else {
|
|
// console.log({publicationDetails})
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
async CreatePublication(folderId, publication: Publication) {
|
|
const response = await this.publications.CreatePublication(folderId, publication).toPromise()
|
|
let publicationDetails: Publication = this.publicationPipe.itemList(response)
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
this.revertPublicationOrder(folderId)
|
|
return publicationDetails
|
|
}
|
|
|
|
async setPublication(publicationId, folderId, publicationDetails: Publication) {
|
|
|
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
if (!found) {
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
this.revertPublicationOrder(folderId)
|
|
} else {
|
|
this.publicationList[folderId][findIndex] = publicationDetails
|
|
}
|
|
|
|
}
|
|
|
|
|
|
async getPublication(publicationId, folderId, publicationDetails: Publication) {
|
|
|
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
if (!found) {
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
this.revertPublicationOrder(folderId);
|
|
|
|
} else {
|
|
return this.publicationList[folderId][findIndex]
|
|
}
|
|
|
|
}
|
|
|
|
|
|
getLocalPublication(folderId, DocumentId) {
|
|
|
|
if (this.publicationList[folderId]) {
|
|
return this.publicationList[folderId].find(e => e.DocumentId == DocumentId)
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
PublicationHasImage(Publication: Publication) {
|
|
return Publication?.Files?.[0]?.FileBase64 != '' && Publication?.Files?.[0]?.FileBase64 != "data:image/jpg;base64,null"
|
|
}
|
|
|
|
hasCapturedImage(Publication: Publication) {
|
|
return Publication?.Files?.[0]?.FileBase64 != '' && Publication?.Files?.[0]?.FileBase64 != "data:image/jpg;base64,null"
|
|
}
|
|
|
|
revertPublicationOrder(folderId) {
|
|
//Revert order of the list
|
|
if (this.publicationList[folderId]) {
|
|
this.publicationList[folderId] = this.publicationList[folderId].sort((a, b) => {
|
|
const dateA = new Date(a.DatePublication).getTime();
|
|
const dateB = new Date(b.DatePublication).getTime();
|
|
return dateB - dateA; // Revertendo a ordem aqui
|
|
})
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|