const { Sequelize, DataTypes } = require('sequelize'); const { sequelize } = require('./model'); const crypto = require('crypto'); const Order = sequelize.define( 'Orders', { order_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, unique: true }, invoice_id: { type: DataTypes.STRING, allowNull: false }, username: { type: DataTypes.STRING, allowNull: false }, custom_id: { type: DataTypes.STRING, allowNull: false }, amount: { type: DataTypes.STRING, allowNull: false }, total_price: { type: DataTypes.DECIMAL, allowNull: false }, currency_code: { type: DataTypes.STRING, allowNull: false }, items: { type: DataTypes.STRING, allowNull: false }, order_status: { type: DataTypes.STRING, allowNull: false, defaultValue: 'NOT_PAID' }, salt: { type: DataTypes.STRING, allowNull: false }, }, { timestamps: true }, ); function genSalt() { return crypto.randomBytes(16).toString('hex'); } function createMD5HashWithSalt(inputString, salt) { // Prepend the salt to the inputString const saltedInput = salt + inputString; // Proceed with the MD5 hash const hash = crypto.createHash('md5').update(saltedInput).digest('hex'); // Optionally, you might want to return both the hash and the salt // for storage, so you can verify the input against the hash later. return { salt, hash }; } (async () => { try { await sequelize.authenticate(); console.log('Connection has been established successfully.'); const item_name = 'item_name'; const quantity = parseInt('10'); const unit_price = parseInt('1'); const currency_code = 'HKD'; const amount = parseInt(20); const total_price = parseInt(20); const email_name = 'cust1@vtkhmall.com'; // create table await Order.drop(); await sequelize.sync(); const order = await Order.create({ invoice_id: crypto.randomUUID(), username: email_name, custom_id: createMD5HashWithSalt( JSON.stringify( [ { item_name, quantity, unit_price }, { item_name, quantity, unit_price }, ], total_price, currency_code, email_name, ), genSalt(), )['hash'], amount, total_price, currency_code, items: JSON.stringify([ { item_name, quantity, unit_price }, { item_name, quantity, unit_price }, ]), order_status: 'NOT_PAID', salt: '12321', }); orders = await Order.findAll(); // await sequelize.close(); } catch (error) { console.error('Unable to connect to the database:', error); } })();