125 lines
4.7 KiB
JavaScript
125 lines
4.7 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.MockDataService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const uuid_1 = require("uuid");
|
|
let MockDataService = class MockDataService {
|
|
constructor() {
|
|
this.subscriptions = new Map();
|
|
this.transactions = new Map();
|
|
this.otpChallenges = new Map();
|
|
this.userBalances = new Map();
|
|
this.initializeMockData();
|
|
}
|
|
initializeMockData() {
|
|
this.userBalances.set('PDKSUB-200-Q82vHq0+F1WozTeNS/1wBfuULco05YBaeL4yPtJ8ktU=', {
|
|
balance: 10000,
|
|
currency: 'CDF',
|
|
type: 'prepaid',
|
|
});
|
|
this.userBalances.set('PDKSUB-200-8Ow1iM0hLPZ+LGZ8j4uwEdQxY1hm4mVwrzTdWiUnuI=', {
|
|
balance: 5000,
|
|
currency: 'TND',
|
|
type: 'prepaid',
|
|
});
|
|
}
|
|
generateISE2(msisdn, country) {
|
|
const base = `PDKSUB-200-`;
|
|
const hash = Buffer.from(`${msisdn}-${country}-${Date.now()}`).toString('base64');
|
|
return base + hash.substring(0, 40);
|
|
}
|
|
createSubscription(data) {
|
|
const subscriptionId = (0, uuid_1.v4)();
|
|
const subscription = {
|
|
id: subscriptionId,
|
|
state: 'Completed',
|
|
orderDate: new Date().toISOString(),
|
|
...data,
|
|
};
|
|
this.subscriptions.set(subscriptionId, subscription);
|
|
return subscription;
|
|
}
|
|
getSubscription(id) {
|
|
return this.subscriptions.get(id);
|
|
}
|
|
deleteSubscription(id) {
|
|
return this.subscriptions.delete(id);
|
|
}
|
|
createTransaction(data) {
|
|
const transactionId = (0, uuid_1.v4)();
|
|
const userBalance = this.getUserBalance(data.endUserId);
|
|
if (userBalance && userBalance.balance >= data.amount) {
|
|
userBalance.balance -= data.amount;
|
|
const transaction = {
|
|
serverReferenceCode: transactionId,
|
|
transactionOperationStatus: 'Charged',
|
|
...data,
|
|
};
|
|
this.transactions.set(transactionId, transaction);
|
|
return { success: true, transaction };
|
|
}
|
|
return {
|
|
success: false,
|
|
error: {
|
|
code: 'POL1000',
|
|
message: 'User has insufficient credit for transaction',
|
|
},
|
|
};
|
|
}
|
|
createOtpChallenge(msisdn, country) {
|
|
const challengeId = (0, uuid_1.v4)();
|
|
const otp = Math.floor(1000 + Math.random() * 9000).toString();
|
|
const challenge = {
|
|
id: challengeId,
|
|
msisdn,
|
|
country,
|
|
otp,
|
|
createdAt: Date.now(),
|
|
attempts: 0,
|
|
};
|
|
this.otpChallenges.set(challengeId, challenge);
|
|
console.log(`[MOCK] OTP généré pour ${msisdn}: ${otp}`);
|
|
return challengeId;
|
|
}
|
|
validateOtp(challengeId, inputOtp) {
|
|
const challenge = this.otpChallenges.get(challengeId);
|
|
if (!challenge) {
|
|
return { success: false, error: 'Challenge not found' };
|
|
}
|
|
challenge.attempts++;
|
|
if (challenge.attempts > 3) {
|
|
return { success: false, error: 'Too many retries' };
|
|
}
|
|
if (Date.now() - challenge.createdAt > 300000) {
|
|
return { success: false, error: 'OTP expired' };
|
|
}
|
|
if (challenge.otp === inputOtp) {
|
|
const ise2 = this.generateISE2(challenge.msisdn, challenge.country);
|
|
this.otpChallenges.delete(challengeId);
|
|
return { success: true, ise2 };
|
|
}
|
|
return { success: false, error: 'Invalid OTP' };
|
|
}
|
|
getUserBalance(ise2) {
|
|
return (this.userBalances.get(ise2) || {
|
|
balance: 1000,
|
|
currency: 'XOF',
|
|
type: 'prepaid',
|
|
});
|
|
}
|
|
};
|
|
exports.MockDataService = MockDataService;
|
|
exports.MockDataService = MockDataService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [])
|
|
], MockDataService);
|
|
//# sourceMappingURL=mock-data.service.js.map
|