update folder of items when folder is deleted

This commit is contained in:
Kyle Spearrin 2018-06-25 14:54:47 -04:00
parent 8a91e5b4d6
commit 1aa774b99f
2 changed files with 21 additions and 9 deletions

View File

@ -26,15 +26,10 @@ import { CipherResponse } from '../models/response/cipherResponse';
import { ErrorResponse } from '../models/response/errorResponse'; import { ErrorResponse } from '../models/response/errorResponse';
import { AttachmentView } from '../models/view/attachmentView'; import { AttachmentView } from '../models/view/attachmentView';
import { CardView } from '../models/view/cardView';
import { CipherView } from '../models/view/cipherView'; import { CipherView } from '../models/view/cipherView';
import { FieldView } from '../models/view/fieldView'; import { FieldView } from '../models/view/fieldView';
import { IdentityView } from '../models/view/identityView';
import { LoginView } from '../models/view/loginView';
import { View } from '../models/view/view'; import { View } from '../models/view/view';
import { ConstantsService } from './constants.service';
import { ApiService } from '../abstractions/api.service'; import { ApiService } from '../abstractions/api.service';
import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service'; import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service';
import { CryptoService } from '../abstractions/crypto.service'; import { CryptoService } from '../abstractions/crypto.service';

View File

@ -9,23 +9,25 @@ import { FolderResponse } from '../models/response/folderResponse';
import { FolderView } from '../models/view/folderView'; import { FolderView } from '../models/view/folderView';
import { ApiService } from '../abstractions/api.service'; import { ApiService } from '../abstractions/api.service';
import { CipherService } from '../abstractions/cipher.service';
import { CryptoService } from '../abstractions/crypto.service'; import { CryptoService } from '../abstractions/crypto.service';
import { FolderService as FolderServiceAbstraction } from '../abstractions/folder.service'; import { FolderService as FolderServiceAbstraction } from '../abstractions/folder.service';
import { I18nService } from '../abstractions/i18n.service'; import { I18nService } from '../abstractions/i18n.service';
import { StorageService } from '../abstractions/storage.service'; import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service'; import { UserService } from '../abstractions/user.service';
import { CipherData } from '../models/data/cipherData';
const Keys = { const Keys = {
foldersPrefix: 'folders_', foldersPrefix: 'folders_',
ciphersPrefix: 'ciphers_',
}; };
export class FolderService implements FolderServiceAbstraction { export class FolderService implements FolderServiceAbstraction {
decryptedFolderCache: FolderView[]; decryptedFolderCache: FolderView[];
constructor(private cryptoService: CryptoService, private userService: UserService, constructor(private cryptoService: CryptoService, private userService: UserService,
private noneFolder: () => string, private apiService: ApiService, private apiService: ApiService, private storageService: StorageService,
private storageService: StorageService, private i18nService: I18nService) { private i18nService: I18nService, private cipherService: CipherService) { }
}
clearCache(): void { clearCache(): void {
this.decryptedFolderCache = null; this.decryptedFolderCache = null;
@ -83,7 +85,7 @@ export class FolderService implements FolderServiceAbstraction {
decFolders.sort(this.getLocaleSortingFunction()); decFolders.sort(this.getLocaleSortingFunction());
const noneFolder = new FolderView(); const noneFolder = new FolderView();
noneFolder.name = this.noneFolder(); noneFolder.name = this.i18nService.t('noneFolder');
decFolders.push(noneFolder); decFolders.push(noneFolder);
this.decryptedFolderCache = decFolders; this.decryptedFolderCache = decFolders;
@ -157,6 +159,21 @@ export class FolderService implements FolderServiceAbstraction {
await this.storageService.save(Keys.foldersPrefix + userId, folders); await this.storageService.save(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null; this.decryptedFolderCache = null;
// Items in a deleted folder are re-assigned to "No Folder"
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(Keys.ciphersPrefix + userId);
if (ciphers != null) {
const updates: CipherData[] = [];
for (const cId in ciphers) {
if (ciphers[cId].folderId === id) {
ciphers[cId].folderId = null;
updates.push(ciphers[cId]);
}
}
if (updates.length > 0) {
this.cipherService.upsert(updates);
}
}
} }
async deleteWithServer(id: string): Promise<any> { async deleteWithServer(id: string): Promise<any> {