(data)
| 164 | } |
| 165 | |
| 166 | async create(data) { |
| 167 | const transaction = await SequelizeRepository.createTransaction(this.options) |
| 168 | |
| 169 | try { |
| 170 | if (TENANT_MODE === TenantMode.SINGLE) { |
| 171 | const count = await TenantRepository.count(null, { |
| 172 | ...this.options, |
| 173 | transaction, |
| 174 | }) |
| 175 | |
| 176 | if (count > 0) { |
| 177 | throw new Error400(this.options.language, 'tenant.exists') |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (data.integrationsRequired) { |
| 182 | // Convert all to lowercase |
| 183 | data.integrationsRequired = data.integrationsRequired.map((item) => item.toLowerCase()) |
| 184 | } |
| 185 | |
| 186 | const record = await TenantRepository.create(data, { |
| 187 | ...this.options, |
| 188 | transaction, |
| 189 | }) |
| 190 | |
| 191 | const segment = await new SegmentService({ |
| 192 | ...this.options, |
| 193 | currentTenant: record, |
| 194 | transaction, |
| 195 | } as IServiceOptions).createProjectGroup({ |
| 196 | name: data.name, |
| 197 | url: data.url, |
| 198 | slug: data.url || (await TenantRepository.generateTenantUrl(data.name, this.options)), |
| 199 | status: SegmentStatus.ACTIVE, |
| 200 | } as SegmentData) |
| 201 | |
| 202 | this.options.currentSegments = [(segment as any).projects[0].subprojects[0]] |
| 203 | |
| 204 | await SettingsService.findOrCreateDefault({ |
| 205 | ...this.options, |
| 206 | currentTenant: record, |
| 207 | transaction, |
| 208 | }) |
| 209 | |
| 210 | const memberAttributeSettingsService = new MemberAttributeSettingsService({ |
| 211 | ...this.options, |
| 212 | currentTenant: record, |
| 213 | }) |
| 214 | |
| 215 | // create default member attribute settings |
| 216 | await memberAttributeSettingsService.createPredefined(DEFAULT_MEMBER_ATTRIBUTES, transaction) |
| 217 | |
| 218 | await TenantUserRepository.create(record, this.options.currentUser, [Roles.values.admin], { |
| 219 | ...this.options, |
| 220 | transaction, |
| 221 | }) |
| 222 | |
| 223 | // create default microservices for the tenant |
no test coverage detected