-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkspacesFactory.ts
More file actions
131 lines (111 loc) · 3.81 KB
/
workspacesFactory.ts
File metadata and controls
131 lines (111 loc) · 3.81 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import AbstractModelFactory from './abstactModelFactory';
import { Collection, Db } from 'mongodb';
import WorkspaceModel from './workspace';
import DataLoaders from '../dataLoaders';
import UserModel from './user';
import PlansFactory from './plansFactory';
import PlanModel from './plan';
import { WorkspaceDBScheme } from '@hawk.so/types';
import { Analytics, AnalyticsEventTypes } from '../utils/analytics';
/**
* Workspaces factory to work with WorkspaceModel
*/
export default class WorkspacesFactory extends AbstractModelFactory<WorkspaceDBScheme, WorkspaceModel> {
/**
* DataBase collection to work with
*/
protected collection: Collection<WorkspaceDBScheme>;
/**
* DataLoaders for fetching data from database
*/
private dataLoaders: DataLoaders;
/**
* Creates workspaces factory instance
* @param dbConnection - connection to DataBase
* @param dataLoaders - dataLoaders for fetching data
*/
constructor(dbConnection: Db, dataLoaders: DataLoaders) {
super(dbConnection, WorkspaceModel);
this.collection = dbConnection.collection('workspaces');
this.dataLoaders = dataLoaders;
}
/**
* Finds workspace by its id
* @param id - user id
*/
public async findById(id: string): Promise<WorkspaceModel | null> {
const workspaceData = await this.dataLoaders.workspaceById.load(id);
if (!workspaceData) {
return null;
}
return new WorkspaceModel(workspaceData);
}
/**
* Creates new workspace in DB
* @param workspaceData - workspace's data
* @param ownerModel - owner of the new workspace
*/
public async create(workspaceData: WorkspaceDBScheme, ownerModel: UserModel): Promise<WorkspaceModel> {
workspaceData = {
...workspaceData,
inviteHash: WorkspaceModel.generateInviteHash(),
};
const workspaceId = (await this.collection.insertOne(workspaceData)).insertedId;
const workspaceModel = new WorkspaceModel({
...workspaceData,
_id: workspaceId,
});
await workspaceModel.addMember(ownerModel._id.toString());
await workspaceModel.grantAdmin(ownerModel._id.toString());
await ownerModel.addWorkspace(workspaceModel._id.toString());
await workspaceModel.changePlan((await this.getDefaultPlan())._id.toString());
await Analytics.logEvent({
event_type: AnalyticsEventTypes.WORKSPACE_CREATED,
user_id: ownerModel._id.toString(),
time: Date.now(),
});
return workspaceModel;
}
/**
* Get Workspaces list by their ids
* @param ids - workspaces ids to fetch
*/
public async findManyByIds(ids: string[]): Promise<WorkspaceModel[]> {
return (await this.dataLoaders.workspaceById.loadMany(ids))
.map((data) => !data || data instanceof Error ? null : new WorkspaceModel(data))
.filter(Boolean) as WorkspaceModel[];
}
/**
* Returns workspace by its subscription id
*
* @param subscriptionId - subscription id from payment system
*/
public async findBySubscriptionId(subscriptionId: string): Promise<WorkspaceModel | null> {
const workspaceData = await this.collection.findOne({ subscriptionId });
return workspaceData && new WorkspaceModel(workspaceData);
}
/**
* Returns workspace by its invite hash
*
* @param inviteHash - workspace invite hash
*/
public async findByInviteHash(inviteHash: string): Promise<WorkspaceModel | null> {
const workspaceData = await this.collection.findOne({
inviteHash,
isRemoved: {
$ne: true,
},
});
if (!workspaceData) {
return null;
}
return workspaceData && new WorkspaceModel(workspaceData);
}
/**
* Get default plan to be used for new projects
*/
private async getDefaultPlan(): Promise<PlanModel> {
const plansFactory = new PlansFactory(this.dbConnection, this.dataLoaders);
return plansFactory.getDefaultPlan();
}
}