2018-02-19 19:07:19 +01:00
|
|
|
import { CollectionData } from '../models/data/collectionData';
|
2018-01-10 05:12:18 +01:00
|
|
|
|
2018-02-19 19:07:37 +01:00
|
|
|
import { Collection } from '../models/domain/collection';
|
2018-01-10 05:12:18 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CollectionView } from '../models/view/collectionView';
|
2018-01-25 20:57:42 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CollectionService as CollectionServiceAbstraction } from '../abstractions/collection.service';
|
|
|
|
import { CryptoService } from '../abstractions/crypto.service';
|
|
|
|
import { I18nService } from '../abstractions/i18n.service';
|
|
|
|
import { StorageService } from '../abstractions/storage.service';
|
|
|
|
import { UserService } from '../abstractions/user.service';
|
2018-01-10 05:12:18 +01:00
|
|
|
|
|
|
|
const Keys = {
|
|
|
|
collectionsPrefix: 'collections_',
|
|
|
|
};
|
|
|
|
|
2018-01-23 23:29:15 +01:00
|
|
|
export class CollectionService implements CollectionServiceAbstraction {
|
2018-01-25 20:57:42 +01:00
|
|
|
decryptedCollectionCache: CollectionView[];
|
2018-01-10 05:12:18 +01:00
|
|
|
|
|
|
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
2018-01-27 04:16:35 +01:00
|
|
|
private storageService: StorageService, private i18nService: I18nService) {
|
2018-01-10 05:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clearCache(): void {
|
|
|
|
this.decryptedCollectionCache = null;
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:38:27 +02:00
|
|
|
async encrypt(model: CollectionView): Promise<Collection> {
|
|
|
|
if (model.organizationId == null) {
|
|
|
|
throw new Error('Collection has no organization id.');
|
|
|
|
}
|
|
|
|
const key = await this.cryptoService.getOrgKey(model.organizationId);
|
|
|
|
if (key == null) {
|
|
|
|
throw new Error('No key for this collection\'s organization.');
|
|
|
|
}
|
|
|
|
const collection = new Collection();
|
|
|
|
collection.id = model.id;
|
|
|
|
collection.organizationId = model.organizationId;
|
|
|
|
collection.readOnly = model.readOnly;
|
|
|
|
collection.name = await this.cryptoService.encrypt(model.name, key);
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2018-07-06 18:40:36 +02:00
|
|
|
async decryptMany(collections: Collection[]): Promise<CollectionView[]> {
|
|
|
|
if (collections == null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const decCollections: CollectionView[] = [];
|
|
|
|
const promises: Array<Promise<any>> = [];
|
|
|
|
collections.forEach((collection) => {
|
|
|
|
promises.push(collection.decrypt().then((c) => decCollections.push(c)));
|
|
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
return decCollections.sort(this.getLocaleSortingFunction());
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:12:18 +01:00
|
|
|
async get(id: string): Promise<Collection> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
|
|
|
Keys.collectionsPrefix + userId);
|
|
|
|
if (collections == null || !collections.hasOwnProperty(id)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Collection(collections[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<Collection[]> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
|
|
|
Keys.collectionsPrefix + userId);
|
|
|
|
const response: Collection[] = [];
|
|
|
|
for (const id in collections) {
|
|
|
|
if (collections.hasOwnProperty(id)) {
|
|
|
|
response.push(new Collection(collections[id]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:57:42 +01:00
|
|
|
async getAllDecrypted(): Promise<CollectionView[]> {
|
2018-01-10 05:12:18 +01:00
|
|
|
if (this.decryptedCollectionCache != null) {
|
|
|
|
return this.decryptedCollectionCache;
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:14:26 +02:00
|
|
|
const hasKey = await this.cryptoService.hasKey();
|
|
|
|
if (!hasKey) {
|
2018-01-10 05:12:18 +01:00
|
|
|
throw new Error('No key.');
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:57:42 +01:00
|
|
|
const collections = await this.getAll();
|
2018-07-06 18:40:36 +02:00
|
|
|
this.decryptedCollectionCache = await this.decryptMany(collections);
|
2018-01-10 05:12:18 +01:00
|
|
|
return this.decryptedCollectionCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
async upsert(collection: CollectionData | CollectionData[]): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
let collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
|
|
|
Keys.collectionsPrefix + userId);
|
|
|
|
if (collections == null) {
|
|
|
|
collections = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection instanceof CollectionData) {
|
|
|
|
const c = collection as CollectionData;
|
|
|
|
collections[c.id] = c;
|
|
|
|
} else {
|
|
|
|
(collection as CollectionData[]).forEach((c) => {
|
|
|
|
collections[c.id] = c;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
|
|
|
this.decryptedCollectionCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async replace(collections: { [id: string]: CollectionData; }): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
|
|
|
this.decryptedCollectionCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async clear(userId: string): Promise<any> {
|
|
|
|
await this.storageService.remove(Keys.collectionsPrefix + userId);
|
|
|
|
this.decryptedCollectionCache = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(id: string | string[]): Promise<any> {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
|
|
|
|
Keys.collectionsPrefix + userId);
|
|
|
|
if (collections == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof id === 'string') {
|
|
|
|
const i = id as string;
|
|
|
|
delete collections[id];
|
|
|
|
} else {
|
|
|
|
(id as string[]).forEach((i) => {
|
|
|
|
delete collections[i];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
|
|
|
this.decryptedCollectionCache = null;
|
|
|
|
}
|
2018-01-27 04:16:35 +01:00
|
|
|
|
2018-07-04 05:33:15 +02:00
|
|
|
getLocaleSortingFunction(): (a: CollectionView, b: CollectionView) => number {
|
2018-01-27 04:16:35 +01:00
|
|
|
return (a, b) => {
|
2018-03-20 00:42:38 +01:00
|
|
|
if (a.name == null && b.name != null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name != null && b.name == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (a.name == null && b.name == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:16:35 +01:00
|
|
|
return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) :
|
|
|
|
a.name.localeCompare(b.name);
|
|
|
|
};
|
|
|
|
}
|
2018-01-10 05:12:18 +01:00
|
|
|
}
|