Skip to content

Commit

Permalink
refactor: checkLoggedIn
Browse files Browse the repository at this point in the history
  • Loading branch information
yoojinko committed Jan 12, 2024
1 parent 64576fa commit dc326dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/controllers/serviceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ export default class ServiceController {
return (await this.ain.getValue(Path.app(serviceName).billingConfig())).depositAddress;
}

isLoggedIn(): void {
if(!this.ain.getDefaultAccount())
throw new Error('You should login First.');
checkLoggedIn(): void {
try {
!this.ain.getAddress();
} catch(error) {
throw new Error('You should login first.');
}
}

async isAdmin(serviceName: string): Promise<void> {
this.isLoggedIn();
this.checkLoggedIn();
const adminPath = `/manage_app/${serviceName}/config/admin`;
const adminList = await this.ain.getValue(adminPath);
if(!adminList[this.ain.getAddress()]) {
Expand Down
14 changes: 7 additions & 7 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class Service {
* @returns {string} Transaction hash.
*/
async chargeCredit(amount: number) {
this.isLoggedIn();
this.checkLoggedIn();
return await this.serviceController.chargeCredit(this.serviceName, amount);
}

Expand All @@ -52,7 +52,7 @@ export default class Service {
* @returns {string} Transaction hash.
*/
async withdrawCredit(amount: number) {
this.isLoggedIn();
this.checkLoggedIn();
return await this.serviceController.withdrawCredit(this.serviceName, amount);
}

Expand All @@ -61,7 +61,7 @@ export default class Service {
* @returns {number} Amount of credit balance.
*/
async getCreditBalance() {
this.isLoggedIn();
this.checkLoggedIn();
return await this.serviceController.getCreditBalance(this.serviceName);
}

Expand All @@ -70,7 +70,7 @@ export default class Service {
* @returns {creditHistories} Histories of credit deposit and usage.
*/
async getCreditHistory() {
this.isLoggedIn();
this.checkLoggedIn();
return await this.serviceController.getCreditHistory(this.serviceName);
}

Expand All @@ -80,7 +80,7 @@ export default class Service {
* @returns {string} Response data from service.
*/
async request(requestData: any, requestKey?: string) {
this.isLoggedIn();
this.checkLoggedIn();
return await this.serviceController.request(this.serviceName, requestData, requestKey);
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export default class Service {
return this.serviceController.isAdmin(this.serviceName);
}

private isLoggedIn() {
return this.serviceController.isLoggedIn();
private checkLoggedIn() {
return this.serviceController.checkLoggedIn();
}
}

0 comments on commit dc326dc

Please sign in to comment.