Skip to content

Commit

Permalink
feat(graph-api): split off graph api and add users class
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin committed Oct 2, 2024
1 parent 5af8b1b commit f3db1fb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
42 changes: 38 additions & 4 deletions src/lib/graph-api/users/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,41 @@ describe('Users', () => {
})
})

describe('getManager', () => {
it(`gets a user's manager by user's userPrincipalName`, async () => {
const user = { id: 'id', userPrincipalName: 'userPrincipalName' }
const manager = { id: 'id', userPrincipalName: 'managerPrincipalName' }
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: manager })
await expect(users.getManager(user.userPrincipalName)).resolves.toEqual(manager)
expect(mockAxios.get).toHaveBeenCalledWith(`users/${user.userPrincipalName}/manager`)
})
})

describe('assignManager', () => {
it('throws an error given user is not found', async () => {
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: null })
const err = new Error('resource not found')
jest.spyOn(mockAxios, 'get').mockRejectedValue(err)
try {
await users.assignManager('user', 'manager')
expect(true).toBe(false)
} catch (error: any) {
expect(error.message).toEqual(
`Attempted to assign user's manager, but no user was found with userPrincipalName "user"`,
`${err.message}: Attempted to assign user's manager, but no user was found with userPrincipalName "user"`,
)
}
})

it('throws an error given manager is not found', async () => {
const err = new Error('resource not found')
jest.spyOn(mockAxios, 'get')
.mockResolvedValueOnce({ data: { id: 'userId' } })
.mockResolvedValueOnce({ data: null })
.mockRejectedValueOnce(err)
try {
await users.assignManager('user', 'manager')
expect(true).toBe(false)
} catch (error: any) {
expect(error.message).toEqual(
`Attempted to assign manager as user's manager, but no user was found with userPrincipalName "manager"`,
`${err.message}: Attempted to assign manager as user's manager, but no user was found with userPrincipalName "manager"`,
)
}
})
Expand All @@ -58,4 +70,26 @@ describe('Users', () => {
})
})
})

describe('removeManager', () => {
it('throws an error given user is not found', async () => {
const err = new Error('resource not found')
jest.spyOn(mockAxios, 'get').mockRejectedValue(err)
try {
await users.removeManager('user')
expect(true).toBe(false)
} catch (error: any) {
expect(error.message).toEqual(
`${err.message}: Attempted to remove user's manager, but no user was found with userPrincipalName "user"`,
)
}
})

it('removes a manager', async () => {
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: { id: 'userId' } })
jest.spyOn(mockAxios, 'delete').mockResolvedValue({ status: 204 })
await users.removeManager('user')
expect(mockAxios.delete).toHaveBeenCalledWith('users/userId/manager/$ref')
})
})
})
53 changes: 46 additions & 7 deletions src/lib/graph-api/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class Users {
constructor(private readonly http: AxiosInstance) {}

/**
* Gets a user by id or userPrincipalNames
* Gets a user by id or userPrincipalName
* https://learn.microsoft.com/en-us/graph/api/user-get
* @param id {id | userPrincipalName}
*/
Expand All @@ -14,6 +14,16 @@ export class Users {
return user
}

/**
* Gets a user's by user's id or userPrincipalName
* https://learn.microsoft.com/en-us/graph/api/user-list-manager
* @param userPrincipalName
*/
async getManager(userPrincipalName: string): Promise<GraphUser> {
const { data: user } = await this.http.get(`users/${userPrincipalName}/manager`)
return user
}

/**
* Assigns a user's manager
* https://learn.microsoft.com/en-us/graph/api/user-post-manager
Expand All @@ -24,22 +34,51 @@ export class Users {
userPrincipalName: string,
managerPrincipalName: string,
): Promise<AxiosResponse<{ status: 204; data: void }>> {
const user = await this.get(userPrincipalName)
if (!user)
let user: GraphUser
let manager: GraphUser

try {
user = await this.get(userPrincipalName)
} catch (e: any) {
throw new Error(
`Attempted to assign ${userPrincipalName}'s manager, but no user was found with ` +
`${e.message}: Attempted to assign ${userPrincipalName}'s manager, but no user was found with ` +
`userPrincipalName "${userPrincipalName}"`,
)
}

const manager = await this.get(managerPrincipalName)
if (!manager)
try {
manager = await this.get(managerPrincipalName)
} catch (e: any) {
throw new Error(
`Attempted to assign ${managerPrincipalName} as ${userPrincipalName}'s manager, ` +
`${e.message}: Attempted to assign ${managerPrincipalName} as ${userPrincipalName}'s manager, ` +
`but no user was found with userPrincipalName "${managerPrincipalName}"`,
)
}

return this.http.put(`users/${user.id}/manager/$ref`, {
'@odata.id': `https://graph.microsoft.com/v1.0/users/${manager.id}`,
})
}

/**
* Removes a user's manager
* https://learn.microsoft.com/en-us/graph/api/user-delete-manager
* @param userPrincipalName
*/
async removeManager(
userPrincipalName: string,
): Promise<AxiosResponse<{ status: 204; data: void }>> {
let user: GraphUser

try {
user = await this.get(userPrincipalName)
} catch (e: any) {
throw new Error(
`${e.message}: Attempted to remove ${userPrincipalName}'s manager, but no user was found with ` +
`userPrincipalName "${userPrincipalName}"`,
)
}

return this.http.delete(`users/${user.id}/manager/$ref`)
}
}

0 comments on commit f3db1fb

Please sign in to comment.