@magic.batua/recharge
Version:
The Recharge module manages all the mobile prepaid, datacard and DTH recharge functionalities of the Magic Batua platform.
71 lines (63 loc) • 2.36 kB
text/typescript
/**
* @module Operator
* @overview Defines the `Operator` class.
*
* @author Animesh Mishra <hello@animesh.ltd>
* @copyright © Animesh Ltd. All Rights Reserved.
*/
/**
* The `Operator` class models a telecom operator and/or other service
* providers that offer products that can be recharged through the Magic
* Batua platform. So prepaid mobile companies, DTH operators and datacard
* companies all are instances of `Operator`.
*/
export class Operator {
/** Operator name, as per Rocket in Pocket. */
public name: string
/** Operator's unique ID, as per Rocket in Pocket */
public code: string
/** Merchant margin percentage as a fraction. E.g. 23% margin would be saved as 0.23. */
public margin: number
/**
* Initialises an `Operator` instance.
* @param {GetOperatorResponse} json The `GetOperatorResponse` is defined in `Source/Operator.ts`.
*/
public constructor(json: GetOperatorResponse) {
this.name = json.operator_name
this.code = json.operator_code
// Margin is reported in the form "xx.xxxx%", we'd like to store it
// as a floating-point number
let fraction = json.margin.substr(0, json.margin.indexOf("%"))
this.margin = Number(fraction)/100.0
}
/**
* Given an array of provider responses from Rocket in Pocket, instantiates and
* returns a `Operator` array.
*/
public static InitList(json: Array<GetOperatorResponse>): Array<Operator> {
let providers = Array<Operator>()
for(var provider of json) {
providers.push(new Operator(provider))
}
return providers
}
/**
* Returns an `Operator` with the given provider `code` amongst the list of
* `operators` given. If no match is found, returns `null`.
*/
public static WithCode(code: string, operators: Array<Operator>): Operator | null {
for (var operator of operators) {
if(operator.code == code) { return operator }
}
return null
}
}
/**
* Response sent by Rocket in Pocket API when requested a list of all operators
* supported by them. The result in an array of `GetOperatorsResponse`.
*/
export interface GetOperatorResponse {
margin: string,
operator_name: string,
operator_code: string
}