@@ -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 */
4445async 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 */
7577async 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 */
8791async 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 */
97104async 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,
0 commit comments