-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MAUI-859: Enable Project Creation #4036
Changes from 10 commits
4b4765e
d554532
5c3231e
7619e5a
5527409
2669d32
7184b59
786a9c9
16acd3d
54c32fd
c2fd4de
8308b06
9fa7f10
be74a20
35e7903
ade59f0
084129a
a361375
0bce588
09304a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
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, | ||
name, | ||
description, | ||
sort_order, | ||
image_url, | ||
logo_url, | ||
permission_groups, | ||
countries, | ||
entityTypes, | ||
default_measure, | ||
dashboard_group_name, | ||
} = this.newRecordData; | ||
|
||
await this.models.wrapInTransaction(async transactingModels => { | ||
await this.createProjectEntity(transactingModels, code, name); | ||
await this.createEntityHierarchy(transactingModels, code, entityTypes); | ||
await this.createProjectEntityRelations(transactingModels, code, countries); | ||
await this.createProjectDashboard(transactingModels, dashboard_group_name, code); | ||
|
||
const { id: projectEntityId } = await transactingModels.entity.findOne({ | ||
'entity.code': code, | ||
}); | ||
const { id: projectEntityHierarchyId } = await transactingModels.entityHierarchy.findOne({ | ||
'entity_hierarchy.name': code, | ||
}); | ||
|
||
const { name: dashboardGroupName } = await transactingModels.dashboard.findOne({ | ||
'dashboard.root_entity_code': code, | ||
}); | ||
|
||
return transactingModels.project.create({ | ||
code, | ||
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, code, name) { | ||
const worldCode = 'World'; | ||
const { id: worldId } = await models.entity.findOne({ 'entity.code': worldCode }); | ||
|
||
await models.entity.create({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: |
||
name, | ||
code, | ||
parent_id: worldId, | ||
type: 'project', | ||
}); | ||
} | ||
|
||
async createProjectEntityRelations(models, code, countries) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest we rename |
||
const { id: projectEntityId } = await models.entity.findOne({ | ||
'entity.code': code, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: you don't need to prefix things here, you can just do |
||
}); | ||
const { id: entityHierarchyId } = await models.entityHierarchy.findOne({ | ||
'entity_hierarchy.name': code, | ||
}); | ||
|
||
for (const countryId of countries) { | ||
const { code: countryCode } = await models.country.findOne({ | ||
'country.id': countryId, | ||
}); | ||
const { id: entityId } = await models.entity.findOne({ | ||
'entity.code': countryCode, | ||
'entity.type': 'country', | ||
}); | ||
await models.entityRelation.create({ | ||
parent_id: projectEntityId, | ||
child_id: entityId, | ||
entity_hierarchy_id: entityHierarchyId, | ||
}); | ||
} | ||
} | ||
|
||
async createProjectDashboard(models, dashboard_group_name, code) { | ||
await models.dashboard.create({ | ||
code: `${code}_project`, | ||
name: dashboard_group_name, | ||
root_entity_code: code, | ||
}); | ||
} | ||
|
||
async createEntityHierarchy(models, code, entityTypes) { | ||
await models.entityHierarchy.create({ | ||
name: code, | ||
canonical_types: entityTypes ? `{${entityTypes.join(',')}}` : '{}', | ||
}); | ||
} | ||
} |
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this endpoint should return all types, including world. There may be another frontend that wants to list it.
You can then either just leave World in the dropdown and leave it to users to ignore it or add this same filter in admin-panel instead (not sure how off the top of my head but should be possible).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to still be the case in this PR, but it's minor