Skip to content

Commit a326c08

Browse files
committed
Add/remove manager functionality
1 parent 4485366 commit a326c08

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

app/lib/organization.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async function getManagers (id) {
3939
*
4040
* @param {object} data - params for an organization
4141
* @param {string} data.name - name of the organization
42+
* @param {int} osmId - osm id of the owner
4243
* @return {promise}
4344
*/
4445
async function create (data, osmId) {
@@ -70,6 +71,7 @@ async function destroy (id) {
7071
* Update an organization
7172
*
7273
* @param {int} id - organization id
74+
* @param {object} data - params for an organization
7375
* @return {promise}
7476
*/
7577
async function update (id, data) {
@@ -82,7 +84,9 @@ async function update (id, data) {
8284
/**
8385
* Add organization owner
8486
*
85-
* @param {int} id - organization owner
87+
* @param {int} id - organization id
88+
* @param {int} osmId - osm id of the owner
89+
* @return {promise}
8690
*/
8791
async function addOwner (id, osmId) {
8892
const conn = await db()
@@ -91,8 +95,11 @@ async function addOwner (id, osmId) {
9195

9296
/**
9397
* Remove organization owner
98+
* There has to be at least one owner for an organization
9499
*
95-
* @param {int} id - organization owner
100+
* @param {int} id - organization id
101+
* @param {int} osmId - osm id of the owner
102+
* @return {promise}
96103
*/
97104
async function removeOwner (id, osmId) {
98105
const conn = await db()
@@ -109,6 +116,37 @@ async function removeOwner (id, osmId) {
109116
return unpack(conn('organization_owner').where({ organization_id: id, osm_id: osmId }).del())
110117
}
111118

119+
/**
120+
* Add organization manager
121+
*
122+
* @param {int} id - organization id
123+
* @param {int} osmId - osm id of the manager
124+
* @return {promise}
125+
*/
126+
async function addManager (id, osmId) {
127+
const conn = await db()
128+
return unpack(conn('organization_manager').insert({ organization_id: id, osm_id: osmId }))
129+
}
130+
131+
/**
132+
* Remove organization manager
133+
* There can be 0 managers in an organization
134+
*
135+
* @param {int} id - organization id
136+
* @param {int} osmId - osm id of the manager
137+
* @return {promise}
138+
*/
139+
async function removeManager (id, osmId) {
140+
const conn = await db()
141+
const managers = map(prop('osm_id'), await getManagers(id))
142+
143+
if (!contains(osmId, managers)) {
144+
throw new Error('osmId is not a manager')
145+
}
146+
147+
return unpack(conn('organization_manager').where({ organization_id: id, osm_id: osmId }).del())
148+
}
149+
112150
/**
113151
* Checks if the osm user is an owner of a team
114152
* @param {int} organizationId - organization id
@@ -144,6 +182,8 @@ module.exports = {
144182
update,
145183
addOwner,
146184
removeOwner,
185+
addManager,
186+
removeManager,
147187
getOwners,
148188
getManagers,
149189
isOwner,

app/tests/api/organization-model.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,47 @@ test('remove owners', async t => {
147147
const error2 = await t.throwsAsync(organization.removeOwner(created.id, user))
148148
t.is(error2.message, 'cannot remove owner because there must be at least one owner')
149149
})
150+
151+
/**
152+
* Test add a manager
153+
* There should be 2 managers because a creator of a team is automatically
154+
* assigned to be a manager
155+
*/
156+
test('add managers', async t => {
157+
// setup
158+
const name = 'add managers'
159+
const user = 1
160+
const user2 = 2
161+
const created = await organization.create({ name }, user)
162+
await organization.addManager(created.id, user2)
163+
164+
// tests
165+
const managers = map(prop('osm_id'), await organization.getManagers(created.id))
166+
t.is(managers.length, 2)
167+
t.true(contains(user, managers))
168+
t.true(contains(user2, managers))
169+
170+
// adding the same manager throws an error
171+
await t.throwsAsync(organization.addManager(created.id, user2))
172+
})
173+
174+
test('remove managers', async t => {
175+
// setup
176+
const name = 'remove managers'
177+
const user = 1
178+
const user2 = 2
179+
const user3 = 3
180+
const created = await organization.create({ name }, user)
181+
await organization.addManager(created.id, user2)
182+
await organization.removeManager(created.id, user2)
183+
184+
// tests
185+
const managers = map(prop('osm_id'), await organization.getManagers(created.id))
186+
t.is(managers.length, 1)
187+
t.true(contains(user, managers))
188+
t.false(contains(user2, managers))
189+
190+
// removing a non manager throws an error
191+
const error = await t.throwsAsync(organization.removeManager(created.id, user3))
192+
t.is(error.message, 'osmId is not a manager')
193+
})

0 commit comments

Comments
 (0)