import { IAppDataType } from "../../server"; import USSDProvider from "./base/USSDProvider"; export class FakeUSSDService extends USSDProvider { public getRouter(appData: IAppDataType) { const router = this.expressRouter(); router.get("/ussd", (req, res) => { res.setHeader("content-type", "text/html; charset=utf-8"); const stdCountry = this.localAppData.config.fallbackCountryCode; const stdCurrency = this.localAppData.config.fallbackCurrency; const stdLanguage = this.localAppData.config.fallbackLanguage; res.status(200).end( ` USSD Emulator
` ); }); router.get("/ussd/:phone", async (req, res) => { res.setHeader("content-type", "text/plain; charset=utf-8"); const input = req.query.input as string; const country = req.query.country as string; const language = req.query.language as string; const currency = req.query.currency as string; const end = req.query.end as string; let rs = ""; let status = 200; if (end === "t") { try { await this.endSession(req.params.phone); } catch (err) { this.logError({ className: "FakeUSSDService", endpoint: "ussd/" + req.params.phone, message: "Could not end USSD session", serious: true, err, }); } } else if (this.hasSession(req.params.phone)) { try { rs = (await this.continueSession(req.params.phone, country, language, currency, input)).message; } catch (err) { this.logError({ className: "FakeUSSDService", endpoint: "ussd/" + req.params.phone, message: "Could not return USSD response", serious: true, err, }); status = 500; rs = (this.localAppData.root.getI18nDataFor(language) || this.localAppData.root.getI18nDataFor("en")).error.INTERNAL_SERVER_ERROR; } } else { try { rs = (await this.startSession(req.params.phone, country, language, currency)).message; } catch (err) { this.logError({ className: "FakeUSSDService", endpoint: "ussd/" + req.params.phone, message: "Could not return USSD response", serious: true, err, }); status = 500; rs = (this.localAppData.root.getI18nDataFor(language) || this.localAppData.root.getI18nDataFor("en")).error.INTERNAL_SERVER_ERROR; } } res.status(status).end(rs); }); return router; } }