ojamoto_models 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/migrations/2026000044-add-field-to-admin-users.js +29 -0
- package/migrations/2026000045-add-fields-for-inventory-on-product.js +55 -0
- package/migrations/2026000046-create-stock-adjustment.js +62 -0
- package/models/AdminUser.js +5 -1
- package/models/Product.js +35 -0
- package/models/StockAdjustment.js +69 -0
- package/package.json +3 -1
- package/seeders/2026000001-seed-super-admin.js +61 -0
- package/services/constants.js +9 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
up: async (queryInterface, Sequelize) => {
|
|
5
|
+
const tableDescription = await queryInterface.describeTable("admin_users");
|
|
6
|
+
|
|
7
|
+
if (!tableDescription.country_calling_code) {
|
|
8
|
+
await queryInterface.addColumn("admin_users", "country_calling_code", {
|
|
9
|
+
type: Sequelize.STRING,
|
|
10
|
+
allowNull: true,
|
|
11
|
+
after: "phone",
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
await queryInterface.changeColumn("admin_users", "phone", {
|
|
16
|
+
type: Sequelize.STRING(10),
|
|
17
|
+
allowNull: false,
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
down: async (queryInterface) => {
|
|
22
|
+
await queryInterface.changeColumn("admin_users", "phone", {
|
|
23
|
+
type: Sequelize.STRING(20),
|
|
24
|
+
allowNull: false,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await queryInterface.removeColumn("admin_users", "country_calling_code");
|
|
28
|
+
},
|
|
29
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async up(queryInterface, Sequelize) {
|
|
5
|
+
await queryInterface.addColumn("products", "low_stock_threshold", {
|
|
6
|
+
type: Sequelize.INTEGER,
|
|
7
|
+
allowNull: false,
|
|
8
|
+
defaultValue: 2,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
await queryInterface.addColumn("products", "units_sold_online", {
|
|
12
|
+
type: Sequelize.INTEGER,
|
|
13
|
+
allowNull: false,
|
|
14
|
+
defaultValue: 0,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
await queryInterface.addColumn("products", "units_sold_offline", {
|
|
18
|
+
type: Sequelize.INTEGER,
|
|
19
|
+
allowNull: false,
|
|
20
|
+
defaultValue: 0,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await queryInterface.addColumn("products", "last_sold_at", {
|
|
24
|
+
type: Sequelize.DATE,
|
|
25
|
+
allowNull: true,
|
|
26
|
+
comment:
|
|
27
|
+
"Set on any real sale (online completion or logged offline sale) — NOT on restock/correction",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await queryInterface.addColumn("products", "low_stock_notified_at", {
|
|
31
|
+
type: Sequelize.DATE,
|
|
32
|
+
allowNull: true,
|
|
33
|
+
comment: "Cleared automatically once stock rises back above threshold",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await queryInterface.addColumn("products", "revalidation_prompted_at", {
|
|
37
|
+
type: Sequelize.DATE,
|
|
38
|
+
allowNull: true,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
async down(queryInterface) {
|
|
43
|
+
await queryInterface.removeColumn("products", "low_stock_threshold");
|
|
44
|
+
|
|
45
|
+
await queryInterface.removeColumn("products", "units_sold_online");
|
|
46
|
+
|
|
47
|
+
await queryInterface.removeColumn("products", "units_sold_offline");
|
|
48
|
+
|
|
49
|
+
await queryInterface.removeColumn("products", "last_sold_at");
|
|
50
|
+
|
|
51
|
+
await queryInterface.removeColumn("products", "low_stock_notified_at");
|
|
52
|
+
|
|
53
|
+
await queryInterface.removeColumn("products", "revalidation_prompted_at");
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async up(queryInterface, Sequelize) {
|
|
5
|
+
await queryInterface.createTable("stock_adjustments", {
|
|
6
|
+
stock_adjustment_id: {
|
|
7
|
+
type: Sequelize.INTEGER,
|
|
8
|
+
primaryKey: true,
|
|
9
|
+
autoIncrement: true,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
},
|
|
12
|
+
product_id: {
|
|
13
|
+
type: Sequelize.INTEGER,
|
|
14
|
+
allowNull: false,
|
|
15
|
+
references: { model: "products", key: "product_id" },
|
|
16
|
+
onDelete: "CASCADE",
|
|
17
|
+
onUpdate: "CASCADE",
|
|
18
|
+
},
|
|
19
|
+
seller_id: {
|
|
20
|
+
type: Sequelize.INTEGER,
|
|
21
|
+
allowNull: false,
|
|
22
|
+
references: { model: "users", key: "user_id" },
|
|
23
|
+
onDelete: "CASCADE",
|
|
24
|
+
onUpdate: "CASCADE",
|
|
25
|
+
comment: "Denormalized for quick per-seller queries",
|
|
26
|
+
},
|
|
27
|
+
type: {
|
|
28
|
+
type: Sequelize.STRING(50),
|
|
29
|
+
allowNull: false,
|
|
30
|
+
},
|
|
31
|
+
quantity_change: {
|
|
32
|
+
type: Sequelize.INTEGER,
|
|
33
|
+
allowNull: false,
|
|
34
|
+
comment: "Signed — negative for reductions",
|
|
35
|
+
},
|
|
36
|
+
previous_stock: {
|
|
37
|
+
type: Sequelize.INTEGER,
|
|
38
|
+
allowNull: false,
|
|
39
|
+
},
|
|
40
|
+
new_stock: {
|
|
41
|
+
type: Sequelize.INTEGER,
|
|
42
|
+
allowNull: false,
|
|
43
|
+
},
|
|
44
|
+
note: {
|
|
45
|
+
type: Sequelize.TEXT,
|
|
46
|
+
allowNull: true,
|
|
47
|
+
},
|
|
48
|
+
created_at: {
|
|
49
|
+
type: Sequelize.DATE,
|
|
50
|
+
allowNull: false,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await queryInterface.addIndex("stock_adjustments", ["product_id"]);
|
|
55
|
+
|
|
56
|
+
await queryInterface.addIndex("stock_adjustments", ["seller_id"]);
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
async down(queryInterface) {
|
|
60
|
+
await queryInterface.dropTable("stock_adjustments");
|
|
61
|
+
},
|
|
62
|
+
};
|
package/models/AdminUser.js
CHANGED
|
@@ -71,10 +71,14 @@ module.exports = (sequelize) => {
|
|
|
71
71
|
validate: { isEmail: true },
|
|
72
72
|
},
|
|
73
73
|
phone: {
|
|
74
|
-
type: DataTypes.STRING(
|
|
74
|
+
type: DataTypes.STRING(10),
|
|
75
75
|
allowNull: false,
|
|
76
76
|
unique: true,
|
|
77
77
|
},
|
|
78
|
+
country_calling_code: {
|
|
79
|
+
type: DataTypes.STRING(3),
|
|
80
|
+
allowNull: false,
|
|
81
|
+
},
|
|
78
82
|
password_hash: {
|
|
79
83
|
type: DataTypes.STRING(255),
|
|
80
84
|
allowNull: false,
|
package/models/Product.js
CHANGED
|
@@ -30,6 +30,11 @@ module.exports = (sequelize) => {
|
|
|
30
30
|
foreignKey: "product_id",
|
|
31
31
|
as: "availabilityReminders",
|
|
32
32
|
});
|
|
33
|
+
|
|
34
|
+
Product.hasMany(models.StockAdjustment, {
|
|
35
|
+
foreignKey: "product_id",
|
|
36
|
+
as: "stockAdjustments",
|
|
37
|
+
});
|
|
33
38
|
}
|
|
34
39
|
}
|
|
35
40
|
|
|
@@ -104,6 +109,36 @@ module.exports = (sequelize) => {
|
|
|
104
109
|
allowNull: false,
|
|
105
110
|
defaultValue: PRODUCT_STATUS.DRAFT,
|
|
106
111
|
},
|
|
112
|
+
low_stock_threshold: {
|
|
113
|
+
type: DataTypes.INTEGER,
|
|
114
|
+
allowNull: false,
|
|
115
|
+
defaultValue: 2,
|
|
116
|
+
},
|
|
117
|
+
units_sold_online: {
|
|
118
|
+
type: DataTypes.INTEGER,
|
|
119
|
+
allowNull: false,
|
|
120
|
+
defaultValue: 0,
|
|
121
|
+
},
|
|
122
|
+
units_sold_offline: {
|
|
123
|
+
type: DataTypes.INTEGER,
|
|
124
|
+
allowNull: false,
|
|
125
|
+
defaultValue: 0,
|
|
126
|
+
},
|
|
127
|
+
last_sold_at: {
|
|
128
|
+
type: DataTypes.DATE,
|
|
129
|
+
allowNull: true,
|
|
130
|
+
comment: "Timestamp of the last sale",
|
|
131
|
+
},
|
|
132
|
+
low_stock_notified_at: {
|
|
133
|
+
type: DataTypes.DATE,
|
|
134
|
+
allowNull: true,
|
|
135
|
+
comment: "Timestamp of the last low stock notification",
|
|
136
|
+
},
|
|
137
|
+
revalidation_prompted_at: {
|
|
138
|
+
type: DataTypes.DATE,
|
|
139
|
+
allowNull: true,
|
|
140
|
+
comment: "Timestamp of the last revalidation prompt",
|
|
141
|
+
},
|
|
107
142
|
},
|
|
108
143
|
{
|
|
109
144
|
sequelize,
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { Model, DataTypes } = require("sequelize");
|
|
4
|
+
const { STOCK_ADJUSTMENT_TYPE } = require("../services/constants");
|
|
5
|
+
|
|
6
|
+
module.exports = (sequelize) => {
|
|
7
|
+
class StockAdjustment extends Model {
|
|
8
|
+
static associate(models) {
|
|
9
|
+
StockAdjustment.belongsTo(models.Product, {
|
|
10
|
+
foreignKey: "product_id",
|
|
11
|
+
as: "product",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
StockAdjustment.belongsTo(models.User, {
|
|
15
|
+
foreignKey: "seller_id",
|
|
16
|
+
as: "seller",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
StockAdjustment.init(
|
|
22
|
+
{
|
|
23
|
+
stock_adjustment_id: {
|
|
24
|
+
type: DataTypes.INTEGER,
|
|
25
|
+
primaryKey: true,
|
|
26
|
+
autoIncrement: true,
|
|
27
|
+
},
|
|
28
|
+
product_id: {
|
|
29
|
+
type: DataTypes.INTEGER,
|
|
30
|
+
allowNull: false,
|
|
31
|
+
},
|
|
32
|
+
seller_id: {
|
|
33
|
+
type: DataTypes.INTEGER,
|
|
34
|
+
allowNull: false,
|
|
35
|
+
},
|
|
36
|
+
type: {
|
|
37
|
+
type: DataTypes.ENUM(...Object.values(STOCK_ADJUSTMENT_TYPE)),
|
|
38
|
+
allowNull: false,
|
|
39
|
+
},
|
|
40
|
+
quantity_change: {
|
|
41
|
+
type: DataTypes.INTEGER,
|
|
42
|
+
allowNull: false,
|
|
43
|
+
},
|
|
44
|
+
previous_stock: {
|
|
45
|
+
type: DataTypes.INTEGER,
|
|
46
|
+
allowNull: false,
|
|
47
|
+
},
|
|
48
|
+
new_stock: {
|
|
49
|
+
type: DataTypes.INTEGER,
|
|
50
|
+
allowNull: false,
|
|
51
|
+
},
|
|
52
|
+
note: {
|
|
53
|
+
type: DataTypes.TEXT,
|
|
54
|
+
allowNull: true,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
sequelize,
|
|
59
|
+
modelName: "StockAdjustment",
|
|
60
|
+
tableName: "stock_adjustments",
|
|
61
|
+
timestamps: true,
|
|
62
|
+
createdAt: "created_at",
|
|
63
|
+
updatedAt: false,
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
StockAdjustment.removeAttribute("id");
|
|
68
|
+
return StockAdjustment;
|
|
69
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ojamoto_models",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"migrate:up": "npx sequelize-cli db:migrate",
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
"homepage": "https://github.com/ojamoto/ojamoto_models#readme",
|
|
23
23
|
"description": "",
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"bcrypt": "^6.0.0",
|
|
26
|
+
"crypto": "^1.0.1",
|
|
25
27
|
"dotenv": "^17.4.2",
|
|
26
28
|
"mysql2": "^3.23.2",
|
|
27
29
|
"sequelize": "^6.37.8",
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const bcrypt = require("bcrypt");
|
|
4
|
+
const crypto = require("crypto");
|
|
5
|
+
const { ADMIN_ROLE, ADMIN_STATUS } = require("../services/constants");
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
async up(queryInterface) {
|
|
9
|
+
const email = process.env.SUPER_ADMIN_EMAIL;
|
|
10
|
+
const tempPassword = process.env.SUPER_ADMIN_TEMP_PASSWORD;
|
|
11
|
+
|
|
12
|
+
if (!email || !tempPassword) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
"SUPER_ADMIN_EMAIL and SUPER_ADMIN_TEMP_PASSWORD must be set in your environment before running this seeder",
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const existing = await queryInterface.sequelize.query(
|
|
19
|
+
`SELECT admin_id FROM admin_users WHERE email = :email LIMIT 1`,
|
|
20
|
+
{
|
|
21
|
+
replacements: { email },
|
|
22
|
+
type: queryInterface.sequelize.QueryTypes.SELECT,
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
if (existing.length > 0) {
|
|
27
|
+
console.log("Super admin already exists — skipping seed");
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const password_hash = await bcrypt.hash(tempPassword, 12);
|
|
32
|
+
|
|
33
|
+
await queryInterface.bulkInsert("admin_users", [
|
|
34
|
+
{
|
|
35
|
+
uuid: crypto.randomUUID(),
|
|
36
|
+
first_name: process.env.SUPER_ADMIN_FIRST_NAME || "Super",
|
|
37
|
+
last_name: process.env.SUPER_ADMIN_LAST_NAME || "Admin",
|
|
38
|
+
email,
|
|
39
|
+
phone: process.env.SUPER_ADMIN_PHONE || "8085202397",
|
|
40
|
+
country_calling_code:
|
|
41
|
+
process.env.SUPER_ADMIN_COUNTRY_CALLING_CODE || "234",
|
|
42
|
+
password_hash,
|
|
43
|
+
must_change_password: true,
|
|
44
|
+
role: ADMIN_ROLE.SUPER_ADMIN,
|
|
45
|
+
status: ADMIN_STATUS.ACTIVE,
|
|
46
|
+
two_factor_enabled: true,
|
|
47
|
+
totp_secret: null,
|
|
48
|
+
created_by: null,
|
|
49
|
+
created_at: new Date(),
|
|
50
|
+
updated_at: new Date(),
|
|
51
|
+
},
|
|
52
|
+
]);
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
async down(queryInterface) {
|
|
56
|
+
const email = process.env.SUPER_ADMIN_EMAIL;
|
|
57
|
+
if (!email) return;
|
|
58
|
+
|
|
59
|
+
await queryInterface.bulkDelete("admin_users", { email });
|
|
60
|
+
},
|
|
61
|
+
};
|
package/services/constants.js
CHANGED
|
@@ -161,4 +161,13 @@ module.exports = {
|
|
|
161
161
|
ACCEPTED: "ACCEPTED",
|
|
162
162
|
REJECTED: "REJECTED",
|
|
163
163
|
},
|
|
164
|
+
|
|
165
|
+
STOCK_ADJUSTMENT_TYPE: {
|
|
166
|
+
RESTOCK: "RESTOCK",
|
|
167
|
+
SALE_OFFLINE: "SALE_OFFLINE",
|
|
168
|
+
DAMAGE: "DAMAGE",
|
|
169
|
+
CORRECTION: "CORRECTION",
|
|
170
|
+
BULK_UPDATE: "BULK_UPDATE",
|
|
171
|
+
OTHER: "OTHER",
|
|
172
|
+
},
|
|
164
173
|
};
|