Last active
February 1, 2024 00:27
-
-
Save AkinAguda/d253f1ac85f4398648b47b9c23c5919c to your computer and use it in GitHub Desktop.
Sample USSD implementation for a friend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PageId = number; | |
type PageOption = { | |
optionText: string; | |
dialIndex: number; | |
pageId: PageId; | |
}; | |
class Page { | |
id: PageId; | |
title: string; | |
pageOptions: PageOption[]; | |
constructor(title: string, options: PageOption[] = []) { | |
this.pageOptions = options; | |
this.id = Math.random(); | |
this.title = title; | |
} | |
addPageOption(option: PageOption) { | |
this.pageOptions.push(option); | |
} | |
getPageOptionByDialindex(dialIndex: number): PageOption | undefined { | |
return this.pageOptions.find( | |
(pageOption) => pageOption.dialIndex === dialIndex | |
); | |
} | |
print() { | |
console.log(` | |
${this.pageOptions.reduce((finalString: string, option) => { | |
finalString = finalString.concat(` | |
${option.dialIndex}. ${option.optionText}`); | |
return finalString; | |
}, `${this.title}`)} | |
`); | |
} | |
} | |
// Code is something like *555* | |
class UssdSystem { | |
code: number; | |
pages: Record<PageId, Page>; | |
rootPageId: PageId; | |
constructor(code: number, rootPage: Page) { | |
this.pages = { ...this.pages, [rootPage.id]: rootPage }; | |
this.rootPageId = rootPage.id; | |
this.code = code; | |
} | |
addPages(pages: Page[]): void { | |
pages.forEach((page) => { | |
this.pages = { ...this.pages, [page.id]: page }; | |
}); | |
} | |
getPageById(id: PageId): Page { | |
return this.pages[id]; | |
} | |
// *number*number*number | |
parsePathAndPrint(path: string) { | |
const dialIndexPath = path.split("*").map((val) => Number(val)); | |
const rootPage = this.pages[this.rootPageId]; | |
let currentPageId: PageId = rootPage.id; | |
for (let i = 0; i < dialIndexPath.length; i++) { | |
const dialIndex = dialIndexPath[i]; | |
const pageOption = | |
this.pages[currentPageId].getPageOptionByDialindex(dialIndex); | |
if (pageOption) { | |
currentPageId = pageOption.pageId; | |
} else { | |
// ERROR HANDLING | |
} | |
} | |
this.pages[currentPageId].print(); | |
} | |
} | |
const createAirtimeBuyingUssd = (): UssdSystem => { | |
const firstPage = new Page("Welcome To Our Service"); | |
const buyAirtimePage = new Page("Buy Airtime Here"); | |
const buyDataPage = new Page("Buy Data Here"); | |
const buyAirtimeFromBankPage = new Page("Buy From Your Bank Here"); | |
const rechargeCardSystem = new UssdSystem(555, firstPage); | |
rechargeCardSystem.addPages([ | |
firstPage, | |
buyAirtimePage, | |
buyDataPage, | |
buyAirtimeFromBankPage, | |
]); | |
/// Start of populating the "first page" | |
firstPage.addPageOption({ | |
dialIndex: 1, | |
optionText: "Buy Airtime", | |
pageId: buyAirtimePage.id, | |
}); | |
firstPage.addPageOption({ | |
dialIndex: 2, | |
optionText: "Buy Data", | |
pageId: buyDataPage.id, | |
}); | |
/// End of populating the first page | |
/// Start of populating the "Buy Airtime page" | |
buyAirtimePage.addPageOption({ | |
dialIndex: 99, | |
optionText: "Go to first page", | |
pageId: firstPage.id, | |
}); | |
buyAirtimePage.addPageOption({ | |
dialIndex: 1, | |
optionText: "Buy From Bank", | |
pageId: buyAirtimeFromBankPage.id, | |
}); | |
/// End of populating the "Buy Airtime page" | |
/// Start of populating the "Buy Airtime page" | |
buyDataPage.addPageOption({ | |
dialIndex: 99, | |
optionText: "Go to first page", | |
pageId: firstPage.id, | |
}); | |
buyDataPage.addPageOption({ | |
dialIndex: 1, | |
optionText: "Buy From Bank", | |
pageId: buyAirtimeFromBankPage.id, | |
}); | |
/// End of populating the "Buy Airtime page" | |
/// Start of populating the "Buy From Your Bank" | |
buyAirtimeFromBankPage.addPageOption({ | |
dialIndex: 99, | |
optionText: "Go to first page", | |
pageId: firstPage.id, | |
}); | |
/// End of populating the "Buy From Your Bank" | |
return rechargeCardSystem; | |
}; | |
const rechargeCardSystem = createAirtimeBuyingUssd(); | |
rechargeCardSystem.parsePathAndPrint("1*1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment