-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommunity.domain.ts
More file actions
74 lines (63 loc) · 3.28 KB
/
Copy pathcommunity.domain.ts
File metadata and controls
74 lines (63 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { DomainDataSource } from "../../data-sources/domain-data-source";
import { CommunityVisa } from "../../domain/contexts/community/community.visa";
import { Community } from "../../domain/contexts/community/community/community";
import { ReadOnlyDomainExecutionContext } from "../../domain/domain-execution-context";
import { CommunityData } from "../../external-dependencies/datastore";
import { CommunityDomainAdapter, EndUserConverter, CommunityConverter, CommunityRepository } from "../../external-dependencies/domain";
import { CommunityCreateInput, CommunityUpdateInput } from "../../external-dependencies/graphql-api";
import { AppContext } from "../../init/app-context-builder";
import { ReadOnlyInfrastructureContext } from "../../init/infrastructure-context";
export interface CommunityDomainApi {
communityCreate(input: CommunityCreateInput) : Promise<CommunityData>;
communityUpdate(input: CommunityUpdateInput) : Promise<CommunityData>;
}
type PropType = CommunityDomainAdapter;
type DomainType = Community<PropType>;
type RepoType = CommunityRepository<PropType>;
export class CommunityDomainApiImpl
extends DomainDataSource<AppContext, CommunityData, PropType, CommunityVisa, DomainType, RepoType>
implements CommunityDomainApi {
async communityCreate(input: CommunityCreateInput): Promise<CommunityData> {
console.log(`communityCreate`, this.context.verifiedUser);
if (this.context.verifiedUser.openIdConfigKey !== 'AccountPortal') {
throw new Error('Unauthorized:communityCreate');
}
let mongoUser = await this.context.applicationServices.users.endUser.dataApi.getUserByExternalId(this.context.verifiedUser.verifiedJWT.sub);
let userDo = new EndUserConverter().toDomain(mongoUser, ReadOnlyInfrastructureContext(), ReadOnlyDomainExecutionContext());
let communityToReturn: CommunityData;
await this.withTransaction(async (repo) => {
let newCommunity = await repo.getNewInstance(
input.name,
userDo);
communityToReturn = new CommunityConverter().toPersistence(await repo.save(newCommunity));
});
return communityToReturn;
}
async communityUpdate(community: CommunityUpdateInput): Promise<CommunityData> {
if (this.context.verifiedUser.openIdConfigKey !== 'AccountPortal') {
throw new Error('Unauthorized');
}
if(community.approvedVendors){
const {approvedVendors} = community;
const vendorIds:string[] = approvedVendors.map((vendor) => vendor.vendorId);
const vendors = await this._context.applicationServices.users.vendorUser.dataApi.getUsers({ _id: { $in: vendorIds } });
if(vendors.length !== approvedVendors.length){
throw new Error('Not all approved vendors exist');
}
}
let result: CommunityData;
await this.withTransaction(async (repo) => {
let domainObject = await repo.get(community.id);
if (!domainObject) {
throw new Error('invalid id');
}
domainObject.Name = (community.name);
domainObject.Domain = (community.domain);
domainObject.WhiteLabelDomain = (community.whiteLabelDomain);
domainObject.Handle = (community.handle);
domainObject.ApprovedVendors = (community.approvedVendors);
result = (new CommunityConverter()).toPersistence(await repo.save(domainObject));
});
return result;
}
}