38433-vm/backend/src/db/models/payments.js
2026-02-14 20:46:34 +00:00

203 lines
2.2 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const payments = sequelize.define(
'payments',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
provider: {
type: DataTypes.ENUM,
values: [
"zarinpal",
"idpay",
"crypto",
"manual"
],
},
provider_reference: {
type: DataTypes.TEXT,
},
authority_code: {
type: DataTypes.TEXT,
},
amount: {
type: DataTypes.DECIMAL,
},
currency: {
type: DataTypes.ENUM,
values: [
"irr",
"usd",
"eur",
"usdt"
],
},
status: {
type: DataTypes.ENUM,
values: [
"initiated",
"pending",
"succeeded",
"failed",
"cancelled",
"refunded"
],
},
payer_info: {
type: DataTypes.TEXT,
},
initiated_at: {
type: DataTypes.DATE,
},
confirmed_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
payments.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.payments.belongsTo(db.orders, {
as: 'order',
foreignKey: {
name: 'orderId',
},
constraints: false,
});
db.payments.hasMany(db.file, {
as: 'receipt_files',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.payments.getTableName(),
belongsToColumn: 'receipt_files',
},
});
db.payments.belongsTo(db.users, {
as: 'createdBy',
});
db.payments.belongsTo(db.users, {
as: 'updatedBy',
});
};
return payments;
};