@@ -6,10 +6,14 @@ import { check } from 'meteor/check'
66import { HTTP } from 'meteor/http'
77import { Random } from 'meteor/random'
88import randToken from 'rand-token'
9- import { addUserToRole , defaultRoleVisibility } from './units'
109import { findOrCreateUser } from './custom-users'
1110import UnitMetaData from './unit-meta-data'
12- import PendingInvitations , { KEEP_DEFAULT , REMOVE_USER , REPLACE_DEFAULT , collectionName as pendingInvitaitonsCollName } from './pending-invitations'
11+ import PendingInvitations , {
12+ KEEP_DEFAULT ,
13+ REMOVE_USER ,
14+ REPLACE_DEFAULT ,
15+ collectionName as pendingInvitationsCollName
16+ } from './pending-invitations'
1317import unitUserInvitedTemplate from '../email-templates/unit-user-invited'
1418import { logger } from '../util/logger'
1519import { getIncrementFor } from './increment-counters'
@@ -42,6 +46,23 @@ export const possibleRoles = [
4246 }
4347]
4448
49+ export const defaultRoleVisibility = {
50+ [ roleEnum . TENANT ] : true ,
51+ [ roleEnum . OWNER_LANDLORD ] : true ,
52+ [ roleEnum . CONTRACTOR ] : true ,
53+ [ roleEnum . MGT_COMPANY ] : true ,
54+ [ roleEnum . AGENT ] : true ,
55+ 'Occupant' : true
56+ }
57+
58+ export const roleSortOrder = [
59+ roleEnum . TENANT ,
60+ roleEnum . OWNER_LANDLORD ,
61+ roleEnum . AGENT ,
62+ roleEnum . MGT_COMPANY ,
63+ roleEnum . CONTRACTOR
64+ ]
65+
4566const UnitRolesData = new Mongo . Collection ( collectionName )
4667
4768const roleDocMemberMatcher = memberId => roleDoc => roleDoc . members . find ( member => member . id === memberId )
@@ -77,6 +98,115 @@ export function inviteUserToRole (invitorId, unitMongoId, inviteeUser, roleType,
7798 return { accessToken }
7899}
79100
101+ export const addUserToRole = (
102+ invitingUser , inviteeUser , unitBzId , role , invType , isOccupant , errorLogParams = { } , doLiveUpdate , isVisible = true ,
103+ isDefaultInvited = true , roleVisibility = defaultRoleVisibility
104+ ) => {
105+ // Filling up role visibility in case some of it is missing
106+ roleVisibility = Object . assign ( { } , defaultRoleVisibility , roleVisibility )
107+
108+ // Creating matching invitation records
109+ const invitationObj = {
110+ invitedBy : invitingUser . bugzillaCreds . id ,
111+ invitee : inviteeUser . bugzillaCreds . id ,
112+ mefeInvitationIdIntValue : getIncrementFor ( pendingInvitationsCollName ) ,
113+ type : invType ,
114+ unitId : unitBzId ,
115+ role,
116+ isOccupant
117+ }
118+
119+ // TODO: Once all dependencies of role resolving are moved from invitations to UnitRolesData, remove this
120+ // Creating the invitation as pending first
121+ const invitationId = PendingInvitations . insert ( invitationObj )
122+
123+ // Linking invitation to user
124+ Meteor . users . update ( inviteeUser . _id , {
125+ $push : {
126+ receivedInvites : {
127+ unitId : invitationObj . unitId ,
128+ invitedBy : invitingUser . _id ,
129+ timestamp : Date . now ( ) ,
130+ type : invitationObj . type ,
131+ invitationId,
132+ role,
133+ isOccupant
134+ }
135+ }
136+ } )
137+
138+ // Adding to the user to a role on BZ using lambda
139+ try {
140+ HTTP . call ( 'POST' , process . env . INVITE_LAMBDA_URL , {
141+ data : [ Object . assign ( { _id : invitationId } , invitationObj ) ] ,
142+ headers : {
143+ Authorization : `Bearer ${ process . env . API_ACCESS_TOKEN } `
144+ }
145+ } )
146+ } catch ( e ) {
147+ logger . error ( {
148+ ...errorLogParams ,
149+ step : 'INVITE lambda request, unit cleanup might be necessary' ,
150+ error : e
151+ } )
152+ throw new Meteor . Error ( 'Invite API Lambda error' , e , { lambdaStatusCode : e . response . statusCode } )
153+ }
154+
155+ // Marking the pending invitation as "done", now that the API responded with success
156+ PendingInvitations . update ( { _id : invitationId } , {
157+ $set : {
158+ done : true
159+ }
160+ } )
161+ Meteor . users . update ( {
162+ _id : inviteeUser . _id ,
163+ 'receivedInvites.invitationId' : invitationId
164+ } , {
165+ $set : {
166+ 'receivedInvites.$.done' : true
167+ }
168+ } )
169+
170+ // Updating the roles collection to sync with BZ's state
171+ const unitRoleQuery = {
172+ roleType : role ,
173+ unitBzId
174+ }
175+
176+ const unitRoleModifier = {
177+ $push : {
178+ members : {
179+ id : inviteeUser . _id ,
180+ isVisible,
181+ isDefaultInvited,
182+ isOccupant,
183+ roleVisibility
184+ }
185+ }
186+ }
187+
188+ UnitRolesData . update ( unitRoleQuery , unitRoleModifier )
189+
190+ // Matching the role if the defaultAssigneeId is not defined and sets it to the current user. Does nothing otherwise
191+ let doForceAssigneeUpdate
192+ switch ( invType ) {
193+ case REPLACE_DEFAULT :
194+ doForceAssigneeUpdate = true
195+ break
196+ default :
197+ doForceAssigneeUpdate = false
198+ }
199+ const assigneeUpdateQuery = doForceAssigneeUpdate ? unitRoleQuery : {
200+ defaultAssigneeId : - 1 ,
201+ ...unitRoleQuery
202+ }
203+ UnitRolesData . update ( assigneeUpdateQuery , {
204+ $set : {
205+ defaultAssigneeId : inviteeUser . _id
206+ }
207+ } )
208+ }
209+
80210export function removeRoleMember ( requestorId , unitBzId , email , errorLogParams ) {
81211 const unitMeta = UnitMetaData . findOne ( { bzId : unitBzId } )
82212 const unitRoles = UnitRolesData . find ( { unitBzId } ) . fetch ( )
@@ -102,7 +232,7 @@ export function removeRoleMember (requestorId, unitBzId, email, errorLogParams)
102232 const invitationObj = {
103233 invitedBy : requestorUser . bugzillaCreds . id ,
104234 invitee : userToRemove . bugzillaCreds . id ,
105- mefeInvitationIdIntValue : getIncrementFor ( pendingInvitaitonsCollName ) ,
235+ mefeInvitationIdIntValue : getIncrementFor ( pendingInvitationsCollName ) ,
106236 type : REMOVE_USER ,
107237 unitId : unitBzId ,
108238 role : toRemoveRole . roleType ,
0 commit comments