deepsite-project / database.js
TheEkopolitan's picture
Build the entire back end stack ready for easy integration from the services flow html
264efb5 verified
Raw History Blame Contribute Delete
2.65 kB
```javascript
// Database configuration and models
const users = [];
const cards = [];
const transactions = [];
const otpCodes = new Map();
// User model
class User {
constructor(id, username, email, phone, password) {
this.id = id;
this.username = username;
this.email = email;
this.phone = phone;
this.password = password;
this.creditScore = 300 + Math.floor(Math.random() * 500);
this.availableCredit = 0;
this.createdAt = new Date().toISOString();
}
}
// Card model
class Card {
constructor(id, userId, label, type, currency, limit) {
this.id = id;
this.userId = userId;
this.label = label;
this.type = type;
this.currency = currency;
this.limit = limit;
this.number = '**** **** **** ' + Math.floor(Math.random() * 10000).toString().padStart(4, '0');
this.expiry = '12/' + (new Date().getFullYear() + 3).toString().slice(-2);
this.cvv = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
this.status = 'active';
this.createdAt = new Date().toISOString();
}
}
// Transaction model
class Transaction {
constructor(id, userId, type, amount, fromCurrency, toCurrency) {
this.id = id;
this.userId = userId;
this.type = type;
this.amount = amount;
this.fromCurrency = fromCurrency;
this.toCurrency = toCurrency;
this.status = 'pending';
this.createdAt = new Date().toISOString();
}
}
// Utility functions
const generateId = () => uuidv4();
const generateCardNumber = () => '**** **** **** ' + Math.floor(Math.random() * 10000).toString().padStart(4, '0');
const generateOTP = () => '123456';
// Data access functions
const findUserById = (id) => users.find(u => u.id === id);
const findUserByEmail = (email) => users.find(u => u.email === email);
const findUserByUsername = (username) => users.find(u => u.username === username);
const createUser = (userData) => {
const user = new User(
generateId(),
userData.username,
userData.email,
userData.phone,
userData.password
);
const createCard = (cardData) => {
const card = new Card(
generateId(),
cardData.userId,
cardData.label,
cardData.type,
cardData.currency,
cardData.limit
);
const createTransaction = (transactionData) => {
const transaction = new Transaction(
generateId(),
transactionData.userId,
transactionData.type,
transactionData.amount,
transactionData.fromCurrency,
transactionData.toCurrency
);
module.exports = {
users,
cards,
transactions,
otpCodes,
findUserById,
findUserByEmail,
findUserByUsername,
createUser,
createCard,
createTransaction
};
```