-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add createConfig and projects endpoint * Add create project endpoint * Add rollback to createProject, write tests * Add rollback transaction * Remove unused import * Pull data from create functions * add validation for whitespace * Adjust config based on changes * Fix error message Co-authored-by: Sima-BES <87400368+Sima-BES@users.noreply.github.com>
- Loading branch information
1 parent
4edddd1
commit 4aacba3
Showing
7 changed files
with
491 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { respond } from '@tupaia/utils'; | ||
|
||
export const GETEntityTypes = async (req, res, next) => { | ||
const { models } = req; | ||
try { | ||
const { types } = await models.entity; | ||
const entityTypes = Object.values(types) | ||
.filter(value => value !== 'world') | ||
.map(value => { | ||
return { entityType: value }; | ||
}); | ||
respond(res, entityTypes); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/central-server/src/apiV2/projects/CreateProject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { snake } from 'case'; | ||
import { CreateHandler } from '../CreateHandler'; | ||
import { assertAnyPermissions, assertBESAdminAccess } from '../../permissions'; | ||
|
||
/** | ||
* Handles POST endpoints: | ||
* - /projects | ||
*/ | ||
|
||
export class CreateProject extends CreateHandler { | ||
async assertUserHasAccess() { | ||
await this.assertPermissions( | ||
assertAnyPermissions([assertBESAdminAccess], 'You need BES Admin to create new projects'), | ||
); | ||
} | ||
|
||
async createRecord() { | ||
const { | ||
code: rawProjectCode, | ||
name, | ||
description, | ||
sort_order, | ||
image_url, | ||
logo_url, | ||
permission_groups, | ||
countries, | ||
entityTypes, | ||
default_measure, | ||
dashboard_group_name, | ||
} = this.newRecordData; | ||
|
||
const projectCode = snake(rawProjectCode); | ||
|
||
await this.models.wrapInTransaction(async transactingModels => { | ||
const { id: projectEntityId } = await this.createProjectEntity( | ||
transactingModels, | ||
projectCode, | ||
name, | ||
); | ||
|
||
const { id: projectEntityHierarchyId } = await this.createEntityHierarchy( | ||
transactingModels, | ||
projectCode, | ||
entityTypes, | ||
); | ||
|
||
await this.createProjectEntityRelations(transactingModels, projectCode, countries); | ||
const { name: dashboardGroupName } = await this.createProjectDashboard( | ||
transactingModels, | ||
dashboard_group_name, | ||
projectCode, | ||
); | ||
|
||
return transactingModels.project.create({ | ||
code: projectCode, | ||
description, | ||
sort_order, | ||
image_url, | ||
logo_url, | ||
permission_groups, | ||
default_measure, | ||
dashboard_group_name: dashboardGroupName, | ||
entity_id: projectEntityId, | ||
entity_hierarchy_id: projectEntityHierarchyId, | ||
}); | ||
}); | ||
} | ||
|
||
async createProjectEntity(models, projectCode, name) { | ||
const worldCode = 'World'; | ||
const { id: worldId } = await models.entity.findOne({ code: worldCode }); | ||
|
||
return models.entity.create({ | ||
name, | ||
code: projectCode, | ||
parent_id: worldId, | ||
type: 'project', | ||
}); | ||
} | ||
|
||
async createProjectEntityRelations(models, projectCode, countries) { | ||
const { id: projectEntityId } = await models.entity.findOne({ | ||
code: projectCode, | ||
}); | ||
const { id: entityHierarchyId } = await models.entityHierarchy.findOne({ | ||
name: projectCode, | ||
}); | ||
|
||
for (const countryId of countries) { | ||
const { code: countryCode } = await models.country.findOne({ | ||
id: countryId, | ||
}); | ||
const { id: entityId } = await models.entity.findOne({ | ||
code: countryCode, | ||
type: 'country', | ||
}); | ||
await models.entityRelation.create({ | ||
parent_id: projectEntityId, | ||
child_id: entityId, | ||
entity_hierarchy_id: entityHierarchyId, | ||
}); | ||
} | ||
} | ||
|
||
async createProjectDashboard(models, dashboard_group_name, projectCode) { | ||
return models.dashboard.create({ | ||
code: `${projectCode}_project`, | ||
name: dashboard_group_name, | ||
root_entity_code: projectCode, | ||
}); | ||
} | ||
|
||
async createEntityHierarchy(models, projectCode, entityTypes) { | ||
return models.entityHierarchy.create({ | ||
name: projectCode, | ||
canonical_types: entityTypes ? `{${entityTypes.join(',')}}` : '{}', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export { CreateProject } from './CreateProject'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.