Skip to content

Commit ecf8158

Browse files
committed
store in schema
1 parent 78428c4 commit ecf8158

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/controllers/subscrptionsContoller.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const axios = require("axios");
2+
const { saveSubscriber } = require("../services/mSpaceService");
23

34
exports.sendSubscription = async (req, res) => {
45
try {
@@ -11,7 +12,7 @@ exports.sendSubscription = async (req, res) => {
1112
const payload = {
1213
applicationId: process.env.MSPACE_APP_ID,
1314
password: process.env.MSPACE_PASSWORD,
14-
subscriberId: subscriberId,
15+
subscriberId: subscriberId,
1516
action: action
1617
};
1718

@@ -132,6 +133,7 @@ exports.requestOtp = async (req, res) => {
132133
exports.verifyOtp = async (req, res) => {
133134
try {
134135
const { referenceNo, otp } = req.body;
136+
const { userID } = req.user;
135137

136138
const payload = {
137139
applicationId: process.env.MSPACE_APP_ID,
@@ -150,6 +152,14 @@ exports.verifyOtp = async (req, res) => {
150152
}
151153
);
152154

155+
if (data.statusCode === "S1000" && data.subscriberId) {
156+
await saveSubscriber({
157+
userID,
158+
subscriberId: data.subscriberId,
159+
subscriptionStatus: data.subscriptionStatus
160+
});
161+
}
162+
153163
res.status(200).json(response.data);
154164
} catch (error) {
155165
res.status(500).json({

src/models/mSpaceModel.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const mongoose = require("mongoose");
2+
3+
const mSpaceSchema = new mongoose.Schema({
4+
userID: {
5+
type: Number,
6+
required: true,
7+
index: true
8+
},
9+
subscriberId: {
10+
type: String,
11+
required: true,
12+
unique: true
13+
},
14+
subscriptionStatus: {
15+
type: String,
16+
default: "UNKNOWN"
17+
},
18+
action: {
19+
type: Number,
20+
default: 0
21+
},
22+
createdAt: {
23+
type: Date,
24+
default: Date.now
25+
}
26+
});
27+
28+
module.exports = mongoose.model("MSpace", mSpaceSchema);

src/services/mSpaceService.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const MSpace = require("../models/mSpaceModel");
2+
3+
async function saveSubscriber({ userID, subscriberId, subscriptionStatus }) {
4+
// Avoid duplicates
5+
const existing = await MSpace.findOne({ subscriberId });
6+
if (existing) return existing;
7+
8+
const record = new MSpace({
9+
userID,
10+
subscriberId,
11+
subscriptionStatus
12+
});
13+
14+
await record.save();
15+
return record;
16+
}
17+
18+
module.exports = { saveSubscriber };

src/services/mSpaceSmsService.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ async function sendSMS({ message, phone }) {
88
password: process.env.MSPACE_PASSWORD,
99
message: message,
1010
destinationAddresses: [phone],
11-
sourceAddress: "77100", // Mobitel provided shortcode
12-
deliveryStatusRequest: "1", // Want delivery report
13-
encoding: "0" // 0 = text message
11+
sourceAddress: "77100",
12+
deliveryStatusRequest: "1",
13+
encoding: "0"
1414
};
1515

1616
const response = await axios.post(

0 commit comments

Comments
 (0)