Skip to content

Commit

Permalink
feat(Implement functionality for a complete USSD request):
Browse files Browse the repository at this point in the history
Adds in-cache session management handler as plugin.
  • Loading branch information
Mango Habanero committed Jan 20, 2023
1 parent 4bc3f15 commit 86c0e98
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/plugins/handler/ussdSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Redis from 'ioredis';
import {
JsonObject,
JsonProperty,
JsonSerializer
} from 'typescript-json-serializer';

import {
CacheAccessor
} from "@utils/helpers/redis";


type SessionData = Record<string, string | number> ;

interface UssdSessionObject {
actorInput: string;
data: SessionData;
lastState: string;
ussdCode: string;
version: number;
}

@JsonObject()
export class UssdSession extends CacheAccessor {
actorInput: string;
@JsonProperty()
data: SessionData;
@JsonProperty()
lastState: string;
@JsonProperty()
ussdCode: string;
@JsonProperty()
version: number;

constructor(cacheClient: Redis, cacheKey: string, serializer: JsonSerializer, UssdSessionObject: UssdSessionObject) {
super(cacheClient, cacheKey, serializer);
this.actorInput = UssdSessionObject.actorInput;
this.data = UssdSessionObject.data;
this.lastState = UssdSessionObject.lastState;
this.ussdCode = UssdSessionObject.ussdCode;
this.version = UssdSessionObject.version;
}

async create(serializer: JsonSerializer, ussdSession: UssdSession, expiration = 180) {
await this.cacheJSONData(serializer.serialize(ussdSession), expiration);
}

async get(cache: Redis, cacheKey: string, serializer: JsonSerializer) {
const serializedSession = await this.getCacheJSONData();
if (serializedSession) {
return serializer.deserialize(serializedSession, UssdSession);
} else {
return serializedSession;
}
}

async update(cache: Redis, cacheKey: string, serializer: JsonSerializer, ussdSession: UssdSession) {
await this.cacheJSONData(serializer.serialize(ussdSession));
}
}

0 comments on commit 86c0e98

Please sign in to comment.