2018-10-16 12:41:46 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-01-31 08:20:15 +01:00
|
|
|
|
2019-01-31 13:40:27 +01:00
|
|
|
import { DataStoreService } from '../core-services/data-store.service';
|
|
|
|
import { OperatorService } from '../core-services/operator.service';
|
|
|
|
import { PersonalNote, PersonalNoteObject, PersonalNoteContent } from '../../shared/models/users/personal-note';
|
|
|
|
import { BaseModel } from '../../shared/models/base/base-model';
|
|
|
|
import { HttpService } from '../core-services/http.service';
|
2019-03-04 14:40:02 +01:00
|
|
|
import { BaseViewModel } from 'app/site/base/base-view-model';
|
2018-10-16 12:41:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-04 14:40:02 +01:00
|
|
|
* Handles saving personal notes.
|
2018-10-16 12:41:46 +02:00
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class PersonalNoteService {
|
|
|
|
/**
|
|
|
|
* The personal note object for the operator
|
|
|
|
*/
|
|
|
|
private personalNoteObject: PersonalNoteObject;
|
|
|
|
|
|
|
|
/**
|
2019-03-04 14:40:02 +01:00
|
|
|
* Watches for changes in the personal note model and the operator.
|
2018-10-16 12:41:46 +02:00
|
|
|
*/
|
|
|
|
public constructor(private operator: OperatorService, private DS: DataStoreService, private http: HttpService) {
|
2019-02-01 13:56:08 +01:00
|
|
|
operator.getUserObservable().subscribe(() => this.updatePersonalNoteObject());
|
2019-05-06 11:44:56 +02:00
|
|
|
this.DS.getChangeObservable(PersonalNote).subscribe(_ => {
|
|
|
|
this.updatePersonalNoteObject();
|
2018-10-16 12:41:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the personal note object and notifies the subscribers.
|
|
|
|
*/
|
|
|
|
private updatePersonalNoteObject(): void {
|
|
|
|
if (this.operator.isAnonymous) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the note for the operator.
|
|
|
|
const operatorId = this.operator.user.id;
|
|
|
|
const objects = this.DS.filter(PersonalNote, pn => pn.user_id === operatorId);
|
|
|
|
this.personalNoteObject = objects.length === 0 ? null : objects[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the personal note for the given model.
|
|
|
|
* @param model The model the content belongs to
|
|
|
|
* @param content The new content.
|
|
|
|
*/
|
2019-03-04 14:40:02 +01:00
|
|
|
public async savePersonalNote(model: BaseModel | BaseViewModel, content: PersonalNoteContent): Promise<void> {
|
2018-10-16 12:41:46 +02:00
|
|
|
const pnObject: Partial<PersonalNoteObject> = this.personalNoteObject || {};
|
|
|
|
if (!pnObject.notes) {
|
|
|
|
pnObject.notes = {};
|
|
|
|
}
|
|
|
|
if (!pnObject.notes[model.collectionString]) {
|
|
|
|
pnObject.notes[model.collectionString] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pnObject.notes[model.collectionString][model.id] = content;
|
2019-04-12 13:23:22 +02:00
|
|
|
this.savePersonalNoteObject(pnObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the 'favorite' status for several models of a type in bulk
|
|
|
|
*
|
|
|
|
* @param models The model the content belongs to
|
|
|
|
* @param star The new 'favorite' status
|
|
|
|
*/
|
|
|
|
public async bulkSetStar(models: (BaseModel | BaseViewModel)[], star: boolean): Promise<void> {
|
|
|
|
if (!models.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pnObject: Partial<PersonalNoteObject> = this.personalNoteObject || {};
|
|
|
|
if (!pnObject.notes) {
|
|
|
|
pnObject.notes = {};
|
|
|
|
}
|
|
|
|
for (const model of models) {
|
|
|
|
if (!pnObject.notes[model.collectionString]) {
|
|
|
|
pnObject.notes[model.collectionString] = {};
|
|
|
|
}
|
|
|
|
if (pnObject.notes[model.collectionString][model.id]) {
|
|
|
|
pnObject.notes[model.collectionString][model.id].star = star;
|
|
|
|
} else {
|
|
|
|
pnObject.notes[model.collectionString][model.id] = { star: star, note: '' };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await this.savePersonalNoteObject(pnObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an updated personal note to the server
|
|
|
|
*
|
|
|
|
* @param pnObject a partial (if new) or complete personal note object
|
|
|
|
*/
|
|
|
|
private async savePersonalNoteObject(pnObject: Partial<PersonalNoteObject>): Promise<void> {
|
2018-10-16 12:41:46 +02:00
|
|
|
if (!pnObject.id) {
|
|
|
|
await this.http.post('rest/users/personal-note/', pnObject);
|
|
|
|
} else {
|
|
|
|
await this.http.put(`rest/users/personal-note/${pnObject.id}/`, pnObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|