-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0362149
commit 54dc76d
Showing
14 changed files
with
440 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { D2Api } from "types/d2-api"; | ||
import { Async } from "domain/entities/Async"; | ||
import { SmsGateway } from "domain/entities/SmsGateway"; | ||
import { SendSmsOptions, SmsOperationResponse, SmsRepository } from "domain/repositories/SmsRepository"; | ||
import { Id } from "domain/entities/Base"; | ||
import { SmsOutbound } from "domain/entities/SmsOutbound"; | ||
import { Pager } from "domain/entities/Pager"; | ||
|
||
export interface D2SmsGatewayParameter { | ||
header: boolean; | ||
encode: boolean; | ||
key: string; | ||
value: string; | ||
confidential: boolean; | ||
} | ||
|
||
export class SmsD2Repository implements SmsRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
async getGateways(): Async<SmsGateway[]> { | ||
const { gateways } = await this.api.get<{ gateways: SmsGateway[] }>("gateways").getData(); | ||
return gateways; | ||
} | ||
|
||
async getGatewayById(uid: Id): Async<SmsGateway> { | ||
const gateway = await this.api.get<SmsGateway>(`gateways/${uid}`).getData(); | ||
return gateway; | ||
} | ||
|
||
async createGateway(gatewayInfo: SmsGateway): Async<SmsOperationResponse> { | ||
const response = await this.api.post<SmsOperationResponse>("gateways", {}, gatewayInfo).getData(); | ||
return response; | ||
} | ||
|
||
async updateGateway(uid: Id, gatewayInfo: SmsGateway): Async<SmsOperationResponse> { | ||
const response = await this.api | ||
.put<SmsOperationResponse>(`gateways/${uid}`, {}, gatewayInfo) | ||
.getData(); | ||
|
||
return response; | ||
} | ||
|
||
async deleteGateway(uid: Id): Async<SmsOperationResponse> { | ||
const response = await this.api.delete<SmsOperationResponse>(`gateways/${uid}`).getData(); | ||
|
||
return response; | ||
} | ||
|
||
async sendSMS(payload: SendSmsOptions): Async<SmsOperationResponse> { | ||
const response = await this.api | ||
.post<SmsOperationResponse>( | ||
"sms/outbound", | ||
{}, | ||
{ | ||
message: payload.message, | ||
recipients: [payload.recipient], | ||
} | ||
) | ||
.getData(); | ||
return response; | ||
} | ||
|
||
async getOutboundSmsList(): Async<SmsOutbound[]> { | ||
const response = await this.api | ||
.get<{ pager: Pager; outboundsmss: SmsOutbound[] }>("sms/outbound", { | ||
fields: "*", | ||
order: "date:desc", | ||
}) | ||
.getData(); | ||
return response.outboundsmss; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { D2SmsGatewayParameter } from "data/SmsD2Repository"; | ||
import { Id } from "./Base"; | ||
|
||
export type SmsGateway = ClickatellGateway | BulkSmsGateway | SMPPGateway | GenericHttpSmsGateway; | ||
|
||
interface SmsGatewayBase { | ||
uid?: Id; | ||
name: string; | ||
isDefault: boolean; | ||
} | ||
|
||
export interface ClickatellGateway extends SmsGatewayBase { | ||
type: "clickatell"; | ||
username: string; | ||
authToken: string; | ||
urlTemplate: string; | ||
} | ||
|
||
export interface BulkSmsGateway extends SmsGatewayBase { | ||
type: "bulksms"; | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export interface SMPPGateway extends SmsGatewayBase { | ||
type: "smpp"; | ||
systemId: string; | ||
host: string; | ||
systemType: string; | ||
numberPlanIndicator: string; | ||
typeOfNumber: string; | ||
bindType: string; | ||
port: number; | ||
password: string; | ||
compressed: boolean; | ||
} | ||
|
||
export interface GenericHttpSmsGateway extends SmsGatewayBase { | ||
type: "http"; | ||
configurationTemplate: string; | ||
useGet: boolean; | ||
sendUrlParameters: boolean; | ||
urlTemplate: string; | ||
contentType: string; | ||
parameters: Array<D2SmsGatewayParameter>; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Id } from "./Base"; | ||
|
||
export interface SmsOutbound { | ||
id: Id; | ||
status: string; | ||
recipients: string[]; | ||
message: string; | ||
date: string; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { Id } from "domain/entities/Base"; | ||
import { SmsGateway } from "domain/entities/SmsGateway"; | ||
import { SmsOutbound } from "domain/entities/SmsOutbound"; | ||
|
||
export interface SendSmsOptions { | ||
message: string; | ||
recipient: string; | ||
} | ||
|
||
export interface SmsOperationResponse { | ||
status: "OK" | "ERROR"; | ||
message: string; | ||
} | ||
|
||
export interface SmsRepository { | ||
getGateways(): Async<SmsGateway[]>; | ||
getGatewayById(uid: Id): Async<SmsGateway>; | ||
createGateway(gatewayInfo: SmsGateway): Async<SmsOperationResponse>; | ||
updateGateway(uid: Id, gatewayInfo: SmsGateway): Async<SmsOperationResponse>; | ||
deleteGateway(uid: Id): Async<SmsOperationResponse>; | ||
|
||
sendSMS(payload: SendSmsOptions): Async<SmsOperationResponse>; | ||
getOutboundSmsList(): Async<SmsOutbound[]>; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/domain/usecases/sms/CreateSmsGatewayFromConfigUseCase.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { SmsRepository } from "domain/repositories/SmsRepository"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import _ from "lodash"; | ||
import { SmsGateway } from "domain/entities/SmsGateway"; | ||
|
||
export class CreateSmsGatewayFromConfigUseCase { | ||
constructor(private smsRepository: SmsRepository) {} | ||
|
||
async execute(configFilePath: string): Async<void> { | ||
const config = JSON.parse(fs.readFileSync(path.join(".", configFilePath), "utf8")); | ||
const newGateway = _.omit(config, ["uid"]) as SmsGateway; | ||
const result = await this.smsRepository.createGateway(newGateway); | ||
console.info(`${result.status}: ${result.message}`); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { Id } from "domain/entities/Base"; | ||
import { SmsRepository } from "domain/repositories/SmsRepository"; | ||
import _ from "lodash"; | ||
|
||
export class DeleteSmsGatewayUseCase { | ||
constructor(private smsRepository: SmsRepository) {} | ||
|
||
async execute(id: Id): Async<void> { | ||
const result = await this.smsRepository.deleteGateway(id); | ||
console.info(`${result.status}: ${result.message}`); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { SmsRepository } from "domain/repositories/SmsRepository"; | ||
|
||
export class SendSmsUseCase { | ||
constructor(private smsRepository: SmsRepository) {} | ||
|
||
async execute(options: SendSmsUseCaseOptions): Async<void> { | ||
const result = await this.smsRepository.sendSMS(options); | ||
console.info(`${result.status}: ${result.message}`); | ||
} | ||
} | ||
|
||
export interface SendSmsUseCaseOptions { | ||
recipient: string; | ||
message: string; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { Id } from "domain/entities/Base"; | ||
import { SmsRepository } from "domain/repositories/SmsRepository"; | ||
|
||
export class ShowSmsGatewayInfoUseCase { | ||
constructor(private smsRepository: SmsRepository) {} | ||
|
||
async execute(id: Id): Async<void> { | ||
const gateway = await this.smsRepository.getGatewayById(id); | ||
console.info(JSON.stringify(gateway, null, 4)); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { SmsRepository } from "domain/repositories/SmsRepository"; | ||
|
||
export class ShowSmsGatewayListUseCase { | ||
constructor(private smsRepository: SmsRepository) {} | ||
|
||
async execute(): Async<void> { | ||
const gateways = await this.smsRepository.getGateways(); | ||
if (gateways.length === 0) { | ||
console.info("No gateways found"); | ||
return; | ||
} | ||
console.table(gateways, ["uid", "isDefault", "type", "name", "urlTemplate"]); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Async } from "domain/entities/Async"; | ||
import { SmsRepository } from "domain/repositories/SmsRepository"; | ||
|
||
export class ShowSmsOutboundListUseCase { | ||
constructor(private smsRepository: SmsRepository) {} | ||
|
||
async execute(): Async<void> { | ||
const outboundMessages = await this.smsRepository.getOutboundSmsList(); | ||
console.table( | ||
outboundMessages.map(m => ({ | ||
...m, | ||
recipients: m.recipients.join(", "), | ||
})), | ||
["date", "status", "recipients", "message"] | ||
); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.