import ApiClient from './ApiClient'; import * as L from './types/Location'; import urls from './urls'; import { ConflictError } from './errors'; export const mapLocationACLProfileResponse = ( p: L.LocationACLProfileResponse, ): L.LocationACLProfile => ({ id: p.id, email: p.email, name: p.name, thumbnailUrl: p.thumbnail_url, domainId: p.domain_id, domainRole: p.domain_role, }); export const mapLocationACLResponse = ( acl: L.LocationACLResponse, ): L.LocationACL => ({ id: acl.id, grantProfile: acl.grant_profile && mapLocationACLProfileResponse(acl.grant_profile), grantDomainId: acl.grant_domain_id, grantScope: acl.grant_scope, grantFolderPath: acl.grant_folder_path, readGrant: acl.read_grant, updateGrant: acl.update_grant, deleteGrant: acl.delete_grant, customAccessGrants: acl.custom_access_grants, shareGrant: acl.share_grant, fullGrant: acl.full_grant, }); export const mapLocationPendingInviteResponse = ( pi: L.LocationACLPendingInviteResponse, ): L.LocationACLPendingInvite => ({ inviteId: pi.invite_id, email: pi.email, name: pi.name, domainRole: pi.domain_role, lastSentAt: pi.last_sent_at, }); export const mapLocationResponse = (l: L.LocationResponse): L.Location => ({ id: l.id, name: l.name, description: l.description, externalId: l.external_id, createdAt: l.created_at, updatedAt: l.updated_at, acl: l.acl.map((acl) => mapLocationACLResponse(acl)), aclPendingInvites: l.acl_pending_invites.map((invite) => mapLocationPendingInviteResponse(invite), ), }); export default class LocationClient { async getLocations(this: ApiClient, query?: string): Promise { const res = await this.requestProtected< L.GetLocationsRequest, L.GetLocationsResponse >({ method: 'GET', url: urls.locations(query), }); return res?.data?.map(mapLocationResponse); } async createLocation( this: ApiClient, params: L.CreateLocationRequest, ): Promise { try { const data = await this.requestProtected< Record, L.CreateLocationResponse >({ method: 'POST', url: urls.locations(), body: { name: params.name, description: params.description, external_id: params.externalId, }, }); return mapLocationResponse(data); } catch (err: unknown) { if (err instanceof ConflictError) { throw new L.LocationExternalIDConflictError(err.message); } throw err; } } async editLocation( this: ApiClient, locationId: string, params: L.CreateLocationRequest, ): Promise { try { const data = await this.requestProtected< Record, L.EditLocationResponse >({ method: 'PATCH', url: urls.location(locationId), body: { name: params.name, description: params.description, external_id: params.externalId, }, }); return mapLocationResponse(data); } catch (err: unknown) { if (err instanceof ConflictError) { throw new L.LocationExternalIDConflictError(err.message); } throw err; } } async deleteLocation(this: ApiClient, locationId: string): Promise { await this.requestProtected< L.DeleteLocationRequest, L.DeleteLocationResponse >({ method: 'DELETE', url: urls.location(locationId), }); } async getPendingLocations(this: ApiClient): Promise { const res = await this.requestProtected< L.GetLocationsRequest, L.GetLocationsResponse >({ method: 'GET', url: urls.pendingLocations(), }); return res?.data?.map(mapLocationResponse); } async inviteUserToLocation( this: ApiClient, locationId: string, params: Partial, ): Promise { const bodyStruct: Partial = {}; if (params.profileId) { params.profileId && (bodyStruct.profile_id = params.profileId); } else if (params.inviteId) { params.inviteId && (bodyStruct.invite_id = params.inviteId); } const data = await this.requestProtected< Partial, L.InviteUserToLocationResponse >({ method: 'POST', url: urls.inviteUserToLocation(locationId), body: bodyStruct, }); return mapLocationResponse(data); } async removeUserFromLocation( this: ApiClient, locationId: string, params: Partial, ): Promise { const bodyStruct: Partial = {}; if (params.profileId) { params.profileId && (bodyStruct.profile_id = params.profileId); } else if (params.inviteId) { params.inviteId && (bodyStruct.invite_id = params.inviteId); } const data = await this.requestProtected< Partial, L.RemoveUserFromLocationResponse >({ method: 'POST', url: urls.removeUserFromLocation(locationId), body: bodyStruct, }); return mapLocationResponse(data); } async moveDevicesToAnotherLocation( this: ApiClient, locationId: string, params: L.MoveDevicesToAnotherLocation, ): Promise { await this.requestProtected< L.MoveDevicesToAnotherLocationRequest, L.MoveDevicesToAnotherLocationResponse >({ method: 'POST', url: urls.moveDevices(locationId), body: { device_ids: params.deviceIds, }, }); } }