Skip to content

Commit

Permalink
feat: add create methods
Browse files Browse the repository at this point in the history
  • Loading branch information
santese committed Jul 24, 2024
1 parent 0b14ad2 commit aa068a6
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/lib/microsoft-partnercenter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,37 @@ describe('Microsoft Partner Center', () => {
expect(result).toEqual(licenses)
expect(mockAxios.get).toHaveBeenCalledWith('/customers/1/subscribedskus')
})

it('should create a customer', async () => {
const customer = { id: '1' }
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data: customer })
const result = await partnerCenter.createCustomer({ id: '1' } as never)
expect(result).toEqual(customer)
expect(mockAxios.post).toHaveBeenCalledWith('/customers', { id: '1' })
})

it('should create a user', async () => {
const user = { id: '1' }
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data: user })
const result = await partnerCenter.createUser('1', { id: '1' } as never)
expect(result).toEqual(user)
expect(mockAxios.post).toHaveBeenCalledWith('/customers/1/users', {
id: '1',
})
})

it('should set user role', async () => {
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data: {} })
const result = await partnerCenter.setUserRole('1', '1', {
Id: '1',
DisplayName: 'test',
UserPrincipalName: 'test',
})
expect(result).toEqual({})
expect(mockAxios.post).toHaveBeenCalledWith('/customers/1/directoryroles/1/usermembers', {
Id: '1',
DisplayName: 'test',
UserPrincipalName: 'test',
})
})
})
35 changes: 33 additions & 2 deletions src/lib/microsoft-partnercenter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AxiosInstance } from 'axios'
import { ApplicationConsent } from './types'
import { ApplicationConsent, CreateUser, SetUserRole, SetUserRoleResponse, User } from './types'
import { Availability } from './types/availabilities.types'
import { IPartnerCenterConfig } from './types/common.types'
import { Customer } from './types/customers.types'
import { CreateCustomer, Customer } from './types/customers.types'
import { Invoice } from './types/invoices.types'
import { OrderLineItem, OrderLineItemOptions, OrderResponse } from './types/orders.types'
import { Sku } from './types/sku.types'
import { Subscription } from './types/subscriptions.types'
import { TokenManager, initializeHttpAndTokenManager } from './utils/http-token-manager'
import { LicenseUsage } from './types/licenses.types'
import { CreateGDAPRelationship, GDAPRelationship } from './types/gdap.types'

export class MicrosoftPartnerCenter {
private readonly httpAgent: AxiosInstance
Expand Down Expand Up @@ -66,6 +67,36 @@ export class MicrosoftPartnerCenter {
return sub
}

async createCustomer(data: CreateCustomer): Promise<Customer> {
const { data: customer } = await this.httpAgent.post('/customers', data)
return customer
}

async createUser(customerId: string, data: CreateUser): Promise<User> {
const { data: user } = await this.httpAgent.post(`/customers/${customerId}/users`, data)
return user
}

async setUserRole(
customerId: string,
roleId: string,
data: SetUserRole,
): Promise<SetUserRoleResponse> {
const { data: userRole } = await this.httpAgent.post(
`/customers/${customerId}/directoryroles/${roleId}/usermembers`,
data,
)
return userRole
}

async createGDAPRelationship(data: CreateGDAPRelationship): Promise<GDAPRelationship> {
const { data: gdapRelationship } = await this.httpAgent.post(
'/tenantRelationships/delegatedAdminRelationships',
data,
)
return gdapRelationship
}

async updateCustomerSubscriptionUsers(
customerId: string,
subscriptionId: string,
Expand Down
26 changes: 26 additions & 0 deletions src/lib/types/customers.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,29 @@ export type Links = LinksBase
export enum RelationshipToPartner {
Reseller = 'reseller',
}

export interface CreateCustomer {
enableGDAPByDefault: boolean
CompanyProfile: {
Domain: string
}
BillingProfile: BillingProfile
}

export interface BillingProfile {
Culture: string
Email: string
Language: string
CompanyName: string
DefaultAddress: DefaultAddress
}

export interface DefaultAddress {
FirstName: string
LastName: string
AddressLine1: string
City: string
State: string
PostalCode: string
Country: string
}
41 changes: 41 additions & 0 deletions src/lib/types/gdap.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export interface CreateGDAPRelationship {
displayName: string
duration: string
customer: GDAPCustomer
accessDetails: AccessDetails
autoExtendDuration: string
}

export interface AccessDetails {
unifiedRoles: UnifiedRole[]
}

export interface UnifiedRole {
roleDefinitionId: string
}

export interface GDAPCustomer {
tenantId: string
displayName: string
}

export interface GDAPRelationship {
'@odata.type': '#microsoft.graph.delegatedAdminRelationship'
'@odata.context': 'https://graph.microsoft.com/v1.0/tenantRelationships/$metadata#delegatedAdminRelationships'
'@odata.etag': string
id: string
displayName: string
duration: string
customer: GDAPCustomer
accessDetails: AccessDetails
status: string
autoExtendDuration: string
createdDateTime: Date
lastModifiedDateTime: Date
activatedDateTime: string
endDateTime: Date
}

export interface AccessDetails {
unifiedRoles: UnifiedRole[]
}
2 changes: 2 additions & 0 deletions src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export * from './sku.types'
export * from './availabilities.types'
export * from './application-consent.types'
export * from './licenses.types'
export * from './users.types'
export * from './gdap.types'
53 changes: 53 additions & 0 deletions src/lib/types/users.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export interface CreateUser {
usageLocation: string
userPrincipalName: string
firstName: string
lastName: string
displayName: string
passwordProfile: PasswordProfile
}

export interface PasswordProfile {
forceChangePassword: boolean
password: string
}

export interface User {
usageLocation: string
id: string
userPrincipalName: string
firstName: string
lastName: string
displayName: string
immutableId: string
passwordProfile: PasswordProfile
lastDirectorySyncTime: null
userDomainType: string
state: string
softDeletionTime: { objectType: 'CustomerUser' }
}

export interface SetUserRole {
/**
* The ID of the user
*/
Id: string
/**
* The Display Name of the user
*/
DisplayName: string
/**
* The User's Principal Name
*/
UserPrincipalName: string
}

export interface SetUserRoleResponse {
displayName: string
userPrincipalName: string
roleId: string
id: string
attributes: {
objectType: 'UserMember'
}
}

0 comments on commit aa068a6

Please sign in to comment.