@telstra/iot-connectivity-manager
Version:
Telstra IoT Connectivity Manager
151 lines (140 loc) • 7.57 kB
text/typescript
/* eslint-disable */
import { AssertionError, HttpClient, IAuthConfigProps } from '@telstra/core';
import { ServiceErrorCode } from '../errors/ErrorCode.js';
import { TGetAllServicesRequest, TGetAllServicesResponse, TGetServiceResponse } from '../types/index.js';
import { Validator } from './Validator.js';
import { Constants } from '../constants/index.js';
import { Schemas } from '../schemas/Schemas.js';
export class Services extends HttpClient {
public constructor(public authConfig?: IAuthConfigProps) {
super(authConfig);
this.instance.defaults.baseURL = Constants.API_URL;
}
private validateIMSIParam(imsi: string) {
const regExp = /^\d{15}$/; // Correct regex: 15 digits, no slashes
if (!regExp.test(imsi)) {
throw new AssertionError({
...ServiceErrorCode.InvalidIMSI,
message: `imsi should be exactly 15 digits, e.g. '123456789012345'`,
});
}
}
/**
* Get a paginated, filtered list of IoT SIM services
* @param params.pageNumber - Current page number.
* @param params.pageSize - Size of the page (default: 100, max: 1000).
* @param params.orderBy - Order fields by.
* @param params.orderDirection - Order direction: 'ASC' or 'DESC'.
* @param params.accountNumber - Filter by account number (string or array).
* @param params.imsi - Filter by IMSI (string, array, or filter object).
* @param params.iccid - Filter by ICCID (string, array, or filter object).
* @param params.msisdn - Filter by MSISDN (string, array, or filter object).
* @param params.serialNumber - Filter by serial number (string, array, or filter object).
* @param params.serviceStatus - Filter by service status (string, array, or filter object).
* @param params.lastConnected - Filter by last connected (string, array, or filter object).
* @param params.createdAt - Filter by created at (string, array, or filter object).
* @param params.updatedAt - Filter by updated at (string, array, or filter object).
* @param params.imei - Filter by IMEI (string, array, or filter object).
* @param params.imeiSv - Filter by IMEI SV (string, array, or filter object).
* @param params.lastBillDate - Filter by last bill date (string, array, or filter object).
* @param params.nextBillDate - Filter by next bill date (string, array, or filter object).
* @param params.totalDataAllocation - Filter by total data allocation (string, array, or filter object).
* @param params.basePlanCode - Filter by base plan code (string, array, or filter object).
* @param params.basePlanDescription - Filter by base plan description (string, array, or filter object).
* @param params.basePlanAllocation - Filter by base plan allocation (string, array, or filter object).
* @param params.basePlanCost - Filter by base plan cost (string, array, or filter object).
* @param params.bonusPlanCode - Filter by bonus plan code (string, array, or filter object).
* @param params.bonusPlanDescription - Filter by bonus plan description (string, array, or filter object).
* @param params.bonusPlanAllocation - Filter by bonus plan allocation (string, array, or filter object).
* @param params.bonusPlanCost - Filter by bonus plan cost (string, array, or filter object).
* @param params.totalData - Filter by total data (string, array, or filter object).
* @param params.uploadData - Filter by upload data (string, array, or filter object).
* @param params.downloadData - Filter by download data (string, array, or filter object).
* @param params.dataCallCount - Filter by data call count (string, array, or filter object).
* @param params.incomingVoiceCallCount - Filter by incoming voice call count (string, array, or filter object).
* @param params.incomingVoiceCallDuration - Filter by incoming voice call duration (string, array, or filter object).
* @param params.outgoingVoiceCallCount - Filter by outgoing voice call count (string, array, or filter object).
* @param params.outgoingVoiceCallDuration - Filter by outgoing voice call duration (string, array, or filter object).
* @param params.incomingSmsCount - Filter by incoming SMS count (string, array, or filter object).
* @param params.outgoingSmsCount - Filter by outgoing SMS count (string, array, or filter object).
* @param params.cellName - Filter by cell name (string, array, or filter object).
* @param params.cellLatitude - Filter by cell latitude (string, array, or filter object).
* @param params.cellLongitude - Filter by cell longitude (string, array, or filter object).
* @param params.groupId - Filter by group ID (string, array, or filter object).
* @param params.groupName - Filter by group name (string, array, or filter object).
* @param params.inSession - Filter by in session (string, array, or filter object).
* @param params.ipAddress - Filter by IP address (string, array, or filter object).
* @param params.blockStatus - Filter by block status (string, array, or filter object).
* @param params.planType - Filter by plan type (string, array, or filter object).
* @param params.sourceSystem - Filter by source system (string, array, or filter object).
* @link https://dev.telstra.com/apis/iot-connectivity-service-api/Endpoints#tag/Services/operation/getServices
* @example
```typescript
import { Services } from '@telstra/icm'
const services = new Services();
services.getAll({
pageSize: 2,
pageNumber: 1,
accountNumber: '<ACCOUNT_NUMBER>',
})
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
});
```
*/
public async getAll(params: TGetAllServicesRequest = {}): Promise<TGetAllServicesResponse> {
try {
const validate = new Validator<TGetAllServicesRequest>(params);
validate.schemaInline(Schemas.GET_ALL_SERVICES);
const result = await this.instance.get('/v1/iot-connectivity/v2/services', { params });
return result;
} catch (error: any) {
if (error.response && error.response.status === 404 && error.code === 'ERR_BAD_REQUEST') {
throw new AssertionError({
...ServiceErrorCode.ICMServiceUnavailable,
});
}
throw error;
}
}
/**
* Get a single IoT SIM service by IMSI
* @param imsi - (Required) Unique identifier for the service.
* @link https://dev.telstra.com/apis/iot-connectivity-service-api/Endpoints#tag/Services/operation/getService
* @example
```typescript
import { Services } from '@telstra/icm'
const s = new Services();
services.getByImsi('<IMSI>')
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
});
```
*/
public async getByImsi(imsi: string): Promise<TGetServiceResponse> {
try {
this.validateIMSIParam(imsi);
const result = await this.instance.get<TGetServiceResponse>(`/v1/iot-connectivity/v2/services/${imsi}`);
return result;
} catch (error: any) {
if (error.response && error.response.status === 404 && error.code && error.code === 'ERR_BAD_REQUEST') {
throw new AssertionError({
...ServiceErrorCode.ICMServiceUnavailable,
});
}
if (error.response && error.response.status === 404) {
throw new AssertionError({
...ServiceErrorCode.NotFoundIMSI,
message: `Service with IMSI ${imsi} not found (404).`,
});
}
throw error;
}
}
}