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

256 lines
7.2 KiB
TypeScript
Raw Normal View History

2023-08-10 16:46:55 +01:00
import { Injectable } from '@angular/core';
import { PublicationFolder } from '../models/publicationfolder';
import { Storage } from '@ionic/storage';
import { Publication } from '../models/publication';
2023-08-18 17:37:11 +01:00
import { PublicationsService } from '../services/publications.service';
import { PublicationPipe } from 'src/app/pipes/publication.pipe';
2023-08-10 16:46:55 +01:00
@Injectable({
providedIn: 'root'
})
export class PublicationFolderService {
2024-01-31 17:13:07 +01:00
publicationList: { [key: string]: Publication[] } = {};
FolderDetails: { [key: string]: PublicationFolder } = {};
2023-08-10 16:46:55 +01:00
2023-08-11 16:38:23 +01:00
restoreFolder: {} = {}
2023-08-10 16:46:55 +01:00
keyName: string
2023-08-18 17:37:11 +01:00
showLoader = true
publicationPipe = new PublicationPipe()
getpublication = [];
2023-08-10 16:46:55 +01:00
constructor(
private storage: Storage,
2023-08-18 17:37:11 +01:00
private publications: PublicationsService,
2024-01-31 17:13:07 +01:00
) { }
2023-08-10 16:46:55 +01:00
2023-08-11 16:38:23 +01:00
createPublicationList(folderId) {
2024-01-31 17:13:07 +01:00
if (!this.publicationList[folderId]) {
2023-08-10 16:46:55 +01:00
this.publicationList[folderId] = []
}
2024-01-31 17:13:07 +01:00
if (!this.FolderDetails[folderId]) {
2023-08-10 16:46:55 +01:00
this.FolderDetails[folderId] = new PublicationFolder();
}
2023-08-11 16:38:23 +01:00
}
getFromDB(folderId: any) {
2024-01-31 17:13:07 +01:00
if (!this.restoreFolder[folderId]) {
2023-08-11 16:38:23 +01:00
this.storage.get(folderId).then((viewPublications) => {
this.publicationList[folderId] = viewPublications
})
2023-08-10 16:46:55 +01:00
2024-01-31 17:13:07 +01:00
this.storage.get(folderId + "name").then((viewPublications) => {
2023-08-11 16:38:23 +01:00
this.FolderDetails[folderId] = viewPublications
})
}
2023-08-11 16:38:23 +01:00
this.restoreFolder[folderId] = true
2023-08-10 16:46:55 +01:00
}
updateFolderDetails(folderId, res) {
this.FolderDetails[folderId] = res
2024-01-31 17:13:07 +01:00
this.storage.set(folderId + "name", res)
2023-08-10 16:46:55 +01:00
}
save(folderId) {
2024-01-31 17:13:07 +01:00
this.storage.set(folderId + "name", this.FolderDetails)
2023-08-10 16:46:55 +01:00
this.storage.set(folderId, this.publicationList[folderId]);
}
publicationIsPresent(publicationId, folderId) {
2024-01-31 17:13:07 +01:00
return this.publicationList[folderId].find(e => e.DocumentId == publicationId)
2023-08-10 16:46:55 +01:00
}
publicationFind(publicationId, folderId) {
2024-01-31 17:13:07 +01:00
return this.publicationList[folderId].find(e => e.DocumentId == publicationId)
2023-08-10 16:46:55 +01:00
}
publicationFindIndex(publicationId, folderId) {
2024-01-31 17:13:07 +01:00
return this.publicationList[folderId].findIndex(e => e.DocumentId == publicationId)
2023-08-10 16:46:55 +01:00
}
PublicationAddOrUpdate(folderId, publicationId, Publication: Publication) {
const findIndex = this.publicationFindIndex(publicationId, folderId)
const found = this.publicationIsPresent(publicationId, folderId)
2024-01-31 17:13:07 +01:00
if (!found) {
2023-08-10 16:46:55 +01:00
this.publicationList[folderId].push(Publication)
2024-01-31 17:13:07 +01:00
this.revertPublicationOrder(folderId);
2023-08-10 16:46:55 +01:00
} else {
this.publicationList[folderId][findIndex] = Publication
}
}
2023-08-24 22:15:56 +01:00
deletePost(folderId: any, DocumentId: any) {
2024-01-31 17:13:07 +01:00
if (this.publicationList[folderId]) {
2024-01-31 17:13:07 +01:00
this.publicationList[folderId] = this.publicationList[folderId].filter(e => e.DocumentId != DocumentId)
2023-08-11 16:38:23 +01:00
this.save(folderId)
}
2023-08-10 16:46:55 +01:00
}
2023-08-11 16:38:23 +01:00
2023-08-18 17:37:11 +01:00
getPublicationDetail(folderId) {
this.publications.GetPresidentialAction(folderId).subscribe(res => {
2023-08-18 17:37:11 +01:00
this.FolderDetails[folderId] = res
2024-01-31 17:13:07 +01:00
this.storage.set(folderId + "name", res)
2023-08-18 17:37:11 +01:00
}, (error) => {
this.showLoader = false;
// this.httpErroHandle.httpStatusHandle(error)
});
}
async getPublicationsIds(folderId) {
this.showLoader = true;
try {
const publicationIds = await this.publications.GetPublicationsList(folderId).toPromise();
2023-08-18 17:37:11 +01:00
this.createPublicationList(folderId)
let loadLater = []
for (let publicationId of publicationIds) {
2024-01-31 17:13:07 +01:00
if (!this.publicationIsPresent(publicationId, folderId)) {
2023-08-18 17:37:11 +01:00
await this.loadPublication(publicationId, folderId)
} else {
loadLater.push(publicationId)
}
}
2024-01-31 17:13:07 +01:00
for (let publicationId of loadLater) {
2023-08-18 17:37:11 +01:00
await this.loadPublication(publicationId, folderId)
}
2024-04-09 09:20:49 +01:00
for (let localPublication of this.publicationList[folderId]) {
const apiPublication = publicationIds.includes(localPublication.DocumentId)
if (!apiPublication) {
this.deletePost(folderId, localPublication.DocumentId)
}
}
2023-08-18 17:37:11 +01:00
this.showLoader = false;
2023-08-18 17:37:11 +01:00
this.storage.set(folderId, this.publicationList[folderId]);
this.getpublication = this.publicationList[folderId];
2024-01-31 17:13:07 +01:00
} catch (error) {
2023-08-18 17:37:11 +01:00
this.showLoader = false;
}
}
async loadPublication(publicationId, folderId) {
2023-11-20 14:12:32 +01:00
let Publication = await this.publications.GetPublicationWithArrayOfFilesById(publicationId).toPromise();
2023-08-18 17:37:11 +01:00
let publicationDetails: Publication = this.publicationPipe.itemList(Publication)
2023-08-18 17:37:11 +01:00
const findIndex = this.publicationFindIndex(publicationId, folderId)
const found = this.publicationIsPresent(publicationId, folderId)
2024-01-31 17:13:07 +01:00
if (!found) {
2023-08-18 17:37:11 +01:00
this.publicationList[folderId].push(publicationDetails)
2024-01-31 17:13:07 +01:00
this.revertPublicationOrder(folderId);
2023-08-18 17:37:11 +01:00
} else {
2024-01-31 17:13:07 +01:00
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
2024-01-31 17:13:07 +01:00
if (JSON.stringify(a) != JSON.stringify(b)) {
2024-04-09 09:20:49 +01:00
console.log({a, b})
this.publicationList[folderId][findIndex] = publicationDetails
} else {
// console.log({publicationDetails})
}
2023-08-18 17:37:11 +01:00
}
2023-08-18 17:37:11 +01:00
}
2023-08-23 09:55:14 +01:00
async CreatePublication(folderId, publication: Publication) {
2024-01-31 17:13:07 +01:00
const response = await this.publications.CreatePublication(folderId, publication).toPromise()
2023-08-23 09:55:14 +01:00
let publicationDetails: Publication = this.publicationPipe.itemList(response)
this.publicationList[folderId].push(publicationDetails)
2024-01-31 17:13:07 +01:00
this.revertPublicationOrder(folderId)
2023-08-23 09:55:14 +01:00
return publicationDetails
}
2023-08-18 17:37:11 +01:00
async setPublication(publicationId, folderId, publicationDetails: Publication) {
2023-08-18 17:37:11 +01:00
const findIndex = this.publicationFindIndex(publicationId, folderId)
const found = this.publicationIsPresent(publicationId, folderId)
2024-01-31 17:13:07 +01:00
if (!found) {
2023-08-18 17:37:11 +01:00
this.publicationList[folderId].push(publicationDetails)
2024-01-31 17:13:07 +01:00
this.revertPublicationOrder(folderId)
2023-08-18 17:37:11 +01:00
} else {
this.publicationList[folderId][findIndex] = publicationDetails
}
2023-08-18 17:37:11 +01:00
}
async getPublication(publicationId, folderId, publicationDetails: Publication) {
2023-08-18 17:37:11 +01:00
const findIndex = this.publicationFindIndex(publicationId, folderId)
const found = this.publicationIsPresent(publicationId, folderId)
2024-01-31 17:13:07 +01:00
if (!found) {
2023-08-18 17:37:11 +01:00
this.publicationList[folderId].push(publicationDetails)
2024-01-31 17:13:07 +01:00
this.revertPublicationOrder(folderId);
2024-04-09 09:20:49 +01:00
2023-08-18 17:37:11 +01:00
} else {
return this.publicationList[folderId][findIndex]
}
2023-08-18 17:37:11 +01:00
}
2024-01-31 17:13:07 +01:00
getLocalPublication(folderId, DocumentId) {
2023-08-18 17:37:11 +01:00
2024-01-31 17:13:07 +01:00
if (this.publicationList[folderId]) {
return this.publicationList[folderId].find(e => e.DocumentId == DocumentId)
2023-08-18 17:37:11 +01:00
} else {
}
2023-08-18 17:37:11 +01:00
}
2023-08-22 15:43:20 +01:00
PublicationHasImage(Publication: Publication) {
2024-01-31 17:13:07 +01:00
return Publication?.Files?.[0]?.FileBase64 != '' && Publication?.Files?.[0]?.FileBase64 != "data:image/jpg;base64,null"
2023-08-22 15:43:20 +01:00
}
hasCapturedImage(Publication: Publication) {
2024-01-31 17:13:07 +01:00
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
})
}
2023-08-22 15:43:20 +01:00
}
2023-08-10 16:46:55 +01:00
}