sync organizations

This commit is contained in:
Kyle Spearrin 2018-05-18 15:26:46 -04:00
parent a421f6e64a
commit bf260819bb
5 changed files with 101 additions and 2 deletions

View File

@ -1,3 +1,6 @@
import { OrganizationData } from '../models/data/organizationData';
import { Organization } from '../models/domain/organization';
export abstract class UserService {
userId: string;
email: string;
@ -10,4 +13,8 @@ export abstract class UserService {
getSecurityStamp: () => Promise<string>;
clear: () => Promise<any>;
isAuthenticated: () => Promise<boolean>;
getOrganization: (id: string) => Promise<Organization>;
getAllOrganizations: () => Promise<Organization[]>;
replaceOrganizations: (organizations: { [id: string]: OrganizationData; }) => Promise<any>;
clearOrganizations: (userId: string) => Promise<any>;
}

View File

@ -0,0 +1,20 @@
import { ProfileOrganizationResponse } from '../response/profileOrganizationResponse';
import { OrganizationUserStatusType } from '../../enums/organizationUserStatusType';
import { OrganizationUserType } from '../../enums/organizationUserType';
export class OrganizationData {
id: string;
name: string;
status: OrganizationUserStatusType;
type: OrganizationUserType;
enabled: boolean;
constructor(response: ProfileOrganizationResponse) {
this.id = response.id;
this.name = response.name;
this.status = response.status;
this.type = response.type;
this.enabled = response.enabled;
}
}

View File

@ -0,0 +1,24 @@
import { OrganizationData } from '../data/organizationData';
import { OrganizationUserStatusType } from '../../enums/organizationUserStatusType';
import { OrganizationUserType } from '../../enums/organizationUserType';
export class Organization {
id: string;
name: string;
status: OrganizationUserStatusType;
type: OrganizationUserType;
enabled: boolean;
constructor(obj?: OrganizationData) {
if (obj == null) {
return;
}
this.id = obj.id;
this.name = obj.name;
this.status = obj.status;
this.type = obj.type;
this.enabled = obj.enabled;
}
}

View File

@ -12,6 +12,7 @@ import { UserService } from '../abstractions/user.service';
import { CipherData } from '../models/data/cipherData';
import { CollectionData } from '../models/data/collectionData';
import { FolderData } from '../models/data/folderData';
import { OrganizationData } from '../models/data/organizationData';
import { CipherResponse } from '../models/response/cipherResponse';
import { CollectionResponse } from '../models/response/collectionResponse';
@ -144,6 +145,13 @@ export class SyncService implements SyncServiceAbstraction {
await this.cryptoService.setEncPrivateKey(response.privateKey);
await this.cryptoService.setOrgKeys(response.organizations);
await this.userService.setSecurityStamp(response.securityStamp);
await this.userService.setSecurityStamp(response.securityStamp);
const organizations: { [id: string]: OrganizationData; } = {};
response.organizations.forEach((o) => {
organizations[o.id] = new OrganizationData(o);
});
return await this.userService.replaceOrganizations(organizations);
}
private async syncFolders(userId: string, response: FolderResponse[]) {

View File

@ -1,14 +1,18 @@
import { StorageService } from '../abstractions/storage.service';
import { TokenService } from '../abstractions/token.service';
import { UserService as UserServiceAbsrtaction } from '../abstractions/user.service';
import { UserService as UserServiceAbstraction } from '../abstractions/user.service';
import { OrganizationData } from '../models/data/organizationData';
import { Organization } from '../models/domain/organization';
const Keys = {
userId: 'userId',
userEmail: 'userEmail',
stamp: 'securityStamp',
organizationsPrefix: 'organizations_',
};
export class UserService implements UserServiceAbsrtaction {
export class UserService implements UserServiceAbstraction {
userId: string;
email: string;
stamp: string;
@ -59,10 +63,13 @@ export class UserService implements UserServiceAbsrtaction {
}
async clear(): Promise<any> {
const userId = await this.getUserId();
await Promise.all([
this.storageService.remove(Keys.userId),
this.storageService.remove(Keys.userEmail),
this.storageService.remove(Keys.stamp),
this.clearOrganizations(userId),
]);
this.userId = this.email = this.stamp = null;
@ -77,4 +84,37 @@ export class UserService implements UserServiceAbsrtaction {
const userId = await this.getUserId();
return userId != null;
}
async getOrganization(id: string): Promise<Organization> {
const userId = await this.getUserId();
const organizations = await this.storageService.get<{ [id: string]: OrganizationData; }>(
Keys.organizationsPrefix + userId);
if (organizations == null || !organizations.hasOwnProperty(id)) {
return null;
}
return new Organization(organizations[id]);
}
async getAllOrganizations(): Promise<Organization[]> {
const userId = await this.getUserId();
const organizations = await this.storageService.get<{ [id: string]: OrganizationData; }>(
Keys.organizationsPrefix + userId);
const response: Organization[] = [];
for (const id in organizations) {
if (organizations.hasOwnProperty(id)) {
response.push(new Organization(organizations[id]));
}
}
return response;
}
async replaceOrganizations(organizations: { [id: string]: OrganizationData; }): Promise<any> {
const userId = await this.getUserId();
await this.storageService.save(Keys.organizationsPrefix + userId, organizations);
}
async clearOrganizations(userId: string): Promise<any> {
await this.storageService.remove(Keys.organizationsPrefix + userId);
}
}