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 {
|
|
|
|
|
|
|
|
|
|
publicationList: {[key: string]: Publication[] } = {};
|
|
|
|
|
FolderDetails: {[key: string]: PublicationFolder } = {};
|
|
|
|
|
|
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-12-04 08:34:57 +01:00
|
|
|
|
2023-08-10 16:46:55 +01:00
|
|
|
constructor(
|
|
|
|
|
private storage: Storage,
|
2023-08-18 17:37:11 +01:00
|
|
|
private publications: PublicationsService,
|
2023-08-10 16:46:55 +01:00
|
|
|
) {}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-10 16:46:55 +01:00
|
|
|
|
2023-08-11 16:38:23 +01:00
|
|
|
createPublicationList(folderId) {
|
2023-08-10 16:46:55 +01:00
|
|
|
if(!this.publicationList[folderId]) {
|
|
|
|
|
this.publicationList[folderId] = []
|
|
|
|
|
}
|
|
|
|
|
if(!this.FolderDetails[folderId]) {
|
|
|
|
|
this.FolderDetails[folderId] = new PublicationFolder();
|
|
|
|
|
}
|
2023-08-11 16:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFromDB(folderId: any) {
|
2023-12-04 08:34:57 +01:00
|
|
|
|
|
|
|
|
|
2023-08-11 16:38:23 +01:00
|
|
|
if(!this.restoreFolder[folderId]) {
|
2023-12-04 08:34:57 +01:00
|
|
|
|
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
|
|
|
|
2023-08-11 16:38:23 +01:00
|
|
|
this.storage.get(folderId+"name").then((viewPublications) => {
|
|
|
|
|
this.FolderDetails[folderId] = viewPublications
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
} else {
|
|
|
|
|
this.publicationList[folderId][findIndex] = Publication
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 22:15:56 +01:00
|
|
|
deletePost(folderId: any, DocumentId: any) {
|
2023-12-04 08:34:57 +01:00
|
|
|
|
|
|
|
|
if(this.publicationList[folderId]) {
|
|
|
|
|
|
2023-08-24 22:15:56 +01:00
|
|
|
this.publicationList[folderId] = this.publicationList[folderId].filter( e => e.DocumentId != DocumentId)
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-11 16:38:23 +01:00
|
|
|
this.save(folderId)
|
2023-12-04 08:34:57 +01:00
|
|
|
}
|
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) {
|
2023-12-04 08:34:57 +01:00
|
|
|
|
|
|
|
|
this.publications.GetPresidentialAction(folderId).subscribe(res => {
|
2023-08-18 17:37:11 +01:00
|
|
|
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();
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
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)
|
|
|
|
|
}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
this.showLoader = false;
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
this.storage.set(folderId, this.publicationList[folderId]);
|
|
|
|
|
this.getpublication = this.publicationList[folderId];
|
|
|
|
|
} catch(error) {
|
|
|
|
|
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-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
|
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
|
|
|
if(!found) {
|
|
|
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
|
|
|
} else {
|
2023-12-04 08:34:57 +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
|
|
|
|
|
|
|
|
|
|
if(JSON.stringify(a) != JSON.stringify(b)) {
|
|
|
|
|
|
|
|
|
|
// console.log({a, b})
|
|
|
|
|
this.publicationList[folderId][findIndex] = publicationDetails
|
|
|
|
|
} else {
|
|
|
|
|
// console.log({publicationDetails})
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-23 09:55:14 +01:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
return publicationDetails
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
async setPublication(publicationId, folderId, publicationDetails: Publication) {
|
|
|
|
|
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
|
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
|
|
|
if(!found) {
|
|
|
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
|
|
|
} else {
|
|
|
|
|
this.publicationList[folderId][findIndex] = publicationDetails
|
|
|
|
|
}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getPublication(publicationId, folderId, publicationDetails: Publication) {
|
|
|
|
|
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
const findIndex = this.publicationFindIndex(publicationId, folderId)
|
|
|
|
|
const found = this.publicationIsPresent(publicationId, folderId)
|
|
|
|
|
if(!found) {
|
|
|
|
|
this.publicationList[folderId].push(publicationDetails)
|
|
|
|
|
} else {
|
|
|
|
|
return this.publicationList[folderId][findIndex]
|
|
|
|
|
}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-24 22:15:56 +01:00
|
|
|
getLocalPublication (folderId, DocumentId) {
|
2023-08-18 17:37:11 +01:00
|
|
|
|
|
|
|
|
if(this.publicationList[folderId]) {
|
2023-08-24 22:15:56 +01:00
|
|
|
return this.publicationList[folderId].find( e => e.DocumentId == DocumentId )
|
2023-08-18 17:37:11 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
}
|
2023-12-04 08:34:57 +01:00
|
|
|
|
2023-08-18 17:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-22 15:43:20 +01:00
|
|
|
|
|
|
|
|
PublicationHasImage(Publication: Publication) {
|
2023-11-29 12:17:52 +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) {
|
2023-11-29 12:17:52 +01:00
|
|
|
return Publication.Files[0].FileBase64 != '' && Publication.Files[0].FileBase64 != "data:image/jpg;base64,null"
|
2023-08-22 15:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-10 16:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|