vr-migrations 1.0.39 → 1.0.41

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.
@@ -25,9 +25,19 @@ module.exports = {
25
25
  onUpdate: "CASCADE",
26
26
  onDelete: "SET NULL", // ← This is SET NULL, not CASCADE
27
27
  },
28
- installmentNumber: {
29
- type: Sequelize.INTEGER,
30
- allowNull: false,
28
+ paymentId: {
29
+ type: Sequelize.UUID,
30
+ allowNull: true,
31
+ references: {
32
+ model: "payments",
33
+ key: "id",
34
+ },
35
+ onUpdate: "CASCADE",
36
+ onDelete: "SET NULL",
37
+ installmentNumber: {
38
+ type: Sequelize.INTEGER,
39
+ allowNull: false,
40
+ },
31
41
  },
32
42
  amount: {
33
43
  type: Sequelize.DECIMAL(10, 2),
@@ -0,0 +1,142 @@
1
+ // src/database/migrations/XXXXXXXXXXXXXX-create-payments-table.js
2
+ "use strict";
3
+
4
+ /** @type {import('sequelize-cli').Migration} */
5
+ module.exports = {
6
+ async up(queryInterface, Sequelize) {
7
+ await queryInterface.createTable("payments", {
8
+ id: {
9
+ type: Sequelize.UUID,
10
+ defaultValue: Sequelize.UUIDV4,
11
+ primaryKey: true,
12
+ allowNull: false,
13
+ },
14
+ userId: {
15
+ type: Sequelize.UUID,
16
+ allowNull: false,
17
+ references: {
18
+ model: "users",
19
+ key: "id",
20
+ },
21
+ onUpdate: "CASCADE",
22
+ onDelete: "RESTRICT",
23
+ },
24
+ devicePaymentPlanId: {
25
+ type: Sequelize.UUID,
26
+ allowNull: true,
27
+ references: {
28
+ model: "device_payment_plans",
29
+ key: "id",
30
+ },
31
+ onUpdate: "CASCADE",
32
+ onDelete: "SET NULL",
33
+ },
34
+ installmentId: {
35
+ type: Sequelize.UUID,
36
+ allowNull: true,
37
+ references: {
38
+ model: "installments",
39
+ key: "id",
40
+ },
41
+ onUpdate: "CASCADE",
42
+ onDelete: "SET NULL",
43
+ },
44
+ idempotencyKeyId: {
45
+ type: Sequelize.UUID,
46
+ allowNull: true,
47
+ references: {
48
+ model: "idempotency_records",
49
+ key: "id",
50
+ },
51
+ onUpdate: "CASCADE",
52
+ onDelete: "SET NULL",
53
+ },
54
+ amount: {
55
+ type: Sequelize.DECIMAL(10, 2),
56
+ allowNull: false,
57
+ },
58
+ currency: {
59
+ type: Sequelize.STRING(3),
60
+ allowNull: false,
61
+ defaultValue: "RWF",
62
+ },
63
+ provider: {
64
+ type: Sequelize.ENUM("MTN_MOMO", "AIRTEL_MONEY", "DPO", "FLUTTERWAVE"),
65
+ allowNull: false,
66
+ },
67
+ providerReference: {
68
+ type: Sequelize.STRING(255),
69
+ allowNull: true,
70
+ },
71
+ status: {
72
+ type: Sequelize.ENUM("PENDING", "SUCCESSFUL", "FAILED", "REFUNDED"),
73
+ allowNull: false,
74
+ defaultValue: "PENDING",
75
+ },
76
+ customerPhone: {
77
+ type: Sequelize.STRING(20),
78
+ allowNull: false,
79
+ },
80
+ customerName: {
81
+ type: Sequelize.STRING(100),
82
+ allowNull: true,
83
+ },
84
+ failureReason: {
85
+ type: Sequelize.TEXT,
86
+ allowNull: true,
87
+ },
88
+ metadata: {
89
+ type: Sequelize.JSONB,
90
+ allowNull: false,
91
+ defaultValue: {},
92
+ },
93
+ settledAt: {
94
+ type: Sequelize.DATE,
95
+ allowNull: true,
96
+ },
97
+ createdAt: {
98
+ type: Sequelize.DATE,
99
+ allowNull: false,
100
+ defaultValue: Sequelize.NOW,
101
+ },
102
+ updatedAt: {
103
+ type: Sequelize.DATE,
104
+ allowNull: false,
105
+ defaultValue: Sequelize.NOW,
106
+ },
107
+ });
108
+
109
+ // Add indexes
110
+ await queryInterface.addIndex("payments", ["userId"], {
111
+ name: "payment_user_idx",
112
+ });
113
+ await queryInterface.addIndex("payments", ["devicePaymentPlanId"], {
114
+ name: "payment_plan_idx",
115
+ });
116
+ await queryInterface.addIndex("payments", ["installmentId"], {
117
+ name: "payment_installment_idx",
118
+ });
119
+ await queryInterface.addIndex("payments", ["status"], {
120
+ name: "payment_status_idx",
121
+ });
122
+ await queryInterface.addIndex("payments", ["providerReference"], {
123
+ name: "payment_provider_ref_idx",
124
+ });
125
+ await queryInterface.addIndex("payments", ["createdAt"], {
126
+ name: "payment_created_at_idx",
127
+ });
128
+
129
+ // Add constraint: exactly one of devicePaymentPlanId or installmentId must be non-null
130
+ // This is handled in the model validation, but can also be added as a CHECK constraint
131
+ await queryInterface.sequelize.query(`
132
+ ALTER TABLE "payments" ADD CONSTRAINT "payments_target_check"
133
+ CHECK (
134
+ (("devicePaymentPlanId" IS NOT NULL)::int + ("installmentId" IS NOT NULL)::int) = 1
135
+ );
136
+ `);
137
+ },
138
+
139
+ async down(queryInterface) {
140
+ await queryInterface.dropTable("payments");
141
+ },
142
+ };
@@ -0,0 +1,123 @@
1
+ // src/database/migrations/XXXXXXXXXXXXXX-create-payment-event-logs-table.js
2
+ "use strict";
3
+
4
+ /** @type {import('sequelize-cli').Migration} */
5
+ module.exports = {
6
+ async up(queryInterface, Sequelize) {
7
+ await queryInterface.createTable("payment_event_logs", {
8
+ id: {
9
+ type: Sequelize.UUID,
10
+ defaultValue: Sequelize.UUIDV4,
11
+ primaryKey: true,
12
+ allowNull: false,
13
+ },
14
+ paymentId: {
15
+ type: Sequelize.UUID,
16
+ allowNull: true,
17
+ references: {
18
+ model: "payments",
19
+ key: "id",
20
+ },
21
+ onUpdate: "CASCADE",
22
+ onDelete: "SET NULL",
23
+ },
24
+ provider: {
25
+ type: Sequelize.STRING(50),
26
+ allowNull: false,
27
+ },
28
+ direction: {
29
+ type: Sequelize.ENUM("OUTGOING_REQUEST", "INCOMING_WEBHOOK"),
30
+ allowNull: false,
31
+ },
32
+ action: {
33
+ type: Sequelize.STRING(50),
34
+ allowNull: false,
35
+ },
36
+ externalId: {
37
+ type: Sequelize.STRING(255),
38
+ allowNull: true,
39
+ },
40
+ requestPayload: {
41
+ type: Sequelize.JSONB,
42
+ allowNull: true,
43
+ },
44
+ responsePayload: {
45
+ type: Sequelize.JSONB,
46
+ allowNull: true,
47
+ },
48
+ statusCode: {
49
+ type: Sequelize.INTEGER,
50
+ allowNull: true,
51
+ },
52
+ status: {
53
+ type: Sequelize.ENUM("PENDING", "SUCCESS", "FAILED", "RETRIED"),
54
+ allowNull: false,
55
+ defaultValue: "PENDING",
56
+ },
57
+ error: {
58
+ type: Sequelize.TEXT,
59
+ allowNull: true,
60
+ },
61
+ durationMs: {
62
+ type: Sequelize.INTEGER,
63
+ allowNull: true,
64
+ },
65
+ retryCount: {
66
+ type: Sequelize.INTEGER,
67
+ allowNull: false,
68
+ defaultValue: 0,
69
+ },
70
+ nextRetryAt: {
71
+ type: Sequelize.DATE,
72
+ allowNull: true,
73
+ },
74
+ processedAt: {
75
+ type: Sequelize.DATE,
76
+ allowNull: true,
77
+ },
78
+ createdAt: {
79
+ type: Sequelize.DATE,
80
+ allowNull: false,
81
+ defaultValue: Sequelize.NOW,
82
+ },
83
+ updatedAt: {
84
+ type: Sequelize.DATE,
85
+ allowNull: false,
86
+ defaultValue: Sequelize.NOW,
87
+ },
88
+ });
89
+
90
+ // Add indexes
91
+ await queryInterface.addIndex("payment_event_logs", ["paymentId"], {
92
+ name: "payment_event_payment_idx",
93
+ });
94
+ await queryInterface.addIndex(
95
+ "payment_event_logs",
96
+ ["provider", "externalId"],
97
+ {
98
+ name: "payment_event_provider_external_idx",
99
+ }
100
+ );
101
+ await queryInterface.addIndex(
102
+ "payment_event_logs",
103
+ ["status", "nextRetryAt"],
104
+ {
105
+ name: "payment_event_retry_idx",
106
+ }
107
+ );
108
+ await queryInterface.addIndex("payment_event_logs", ["createdAt"], {
109
+ name: "payment_event_created_at_idx",
110
+ });
111
+ await queryInterface.addIndex(
112
+ "payment_event_logs",
113
+ ["direction", "action"],
114
+ {
115
+ name: "payment_event_direction_action_idx",
116
+ }
117
+ );
118
+ },
119
+
120
+ async down(queryInterface) {
121
+ await queryInterface.dropTable("payment_event_logs");
122
+ },
123
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vr-migrations",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
4
4
  "description": "Database migration and seeding package for VR applications",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1,106 +0,0 @@
1
- "use strict";
2
-
3
- module.exports = {
4
- async up(queryInterface, Sequelize) {
5
- await queryInterface.createTable("payments", {
6
- id: {
7
- type: Sequelize.UUID,
8
- defaultValue: Sequelize.UUIDV4,
9
- primaryKey: true,
10
- allowNull: false,
11
- },
12
-
13
- userId: {
14
- type: Sequelize.UUID,
15
- allowNull: false,
16
- references: {
17
- model: "users",
18
- key: "id",
19
- },
20
- onUpdate: "CASCADE",
21
- onDelete: "RESTRICT",
22
- },
23
-
24
- devicePaymentPlanId: {
25
- type: Sequelize.UUID,
26
- allowNull: false,
27
- references: {
28
- model: "device_payment_plans",
29
- key: "id",
30
- },
31
- onUpdate: "CASCADE",
32
- onDelete: "RESTRICT",
33
- },
34
-
35
- transactionId: {
36
- type: Sequelize.UUID,
37
- allowNull: true,
38
- },
39
-
40
- idempotencyKeyId: {
41
- type: Sequelize.UUID,
42
- allowNull: true,
43
- references: {
44
- model: "idempotency_records",
45
- key: "id",
46
- },
47
- onUpdate: "CASCADE",
48
- onDelete: "SET NULL",
49
- },
50
-
51
- amount: {
52
- type: Sequelize.FLOAT,
53
- allowNull: false,
54
- },
55
-
56
- provider: {
57
- type: Sequelize.ENUM("mtn_momo", "airtel_money"),
58
- allowNull: false,
59
- },
60
-
61
- providerReference: {
62
- type: Sequelize.STRING,
63
- allowNull: true,
64
- },
65
-
66
- status: {
67
- type: Sequelize.ENUM("PENDING", "SUCCESSFUL", "FAILED"),
68
- allowNull: false,
69
- defaultValue: "PENDING",
70
- },
71
-
72
- metadata: {
73
- type: Sequelize.JSONB,
74
- allowNull: false,
75
- defaultValue: {},
76
- },
77
-
78
- createdAt: {
79
- type: Sequelize.DATE,
80
- allowNull: false,
81
- defaultValue: Sequelize.fn("NOW"),
82
- },
83
-
84
- updatedAt: {
85
- type: Sequelize.DATE,
86
- allowNull: false,
87
- defaultValue: Sequelize.fn("NOW"),
88
- },
89
- });
90
-
91
- await queryInterface.addIndex("payments", ["userId"]);
92
- await queryInterface.addIndex("payments", ["devicePaymentPlanId"]);
93
- await queryInterface.addIndex("payments", ["status"]);
94
- await queryInterface.addIndex("payments", ["provider"]);
95
- },
96
-
97
- async down(queryInterface) {
98
- await queryInterface.dropTable("payments");
99
- await queryInterface.sequelize.query(
100
- 'DROP TYPE IF EXISTS "enum_payments_provider";'
101
- );
102
- await queryInterface.sequelize.query(
103
- 'DROP TYPE IF EXISTS "enum_payments_status";'
104
- );
105
- },
106
- };
@@ -1,80 +0,0 @@
1
- "use strict";
2
-
3
- module.exports = {
4
- async up(queryInterface, Sequelize) {
5
- await queryInterface.createTable("transactions", {
6
- id: {
7
- type: Sequelize.UUID,
8
- defaultValue: Sequelize.UUIDV4,
9
- primaryKey: true,
10
- allowNull: false
11
- },
12
-
13
- userId: {
14
- type: Sequelize.UUID,
15
- allowNull: false,
16
- references: {
17
- model: "users",
18
- key: "id"
19
- },
20
- onUpdate: "CASCADE",
21
- onDelete: "RESTRICT"
22
- },
23
-
24
- paymentId: {
25
- type: Sequelize.UUID,
26
- allowNull: false,
27
- references: {
28
- model: "payments",
29
- key: "id"
30
- },
31
- onUpdate: "CASCADE",
32
- onDelete: "RESTRICT"
33
- },
34
-
35
- amount: {
36
- type: Sequelize.FLOAT,
37
- allowNull: false
38
- },
39
-
40
- status: {
41
- type: Sequelize.ENUM("succeeded", "failed"),
42
- allowNull: false
43
- },
44
-
45
- providerReference: {
46
- type: Sequelize.STRING,
47
- allowNull: false
48
- },
49
-
50
- metadata: {
51
- type: Sequelize.JSONB,
52
- allowNull: false,
53
- defaultValue: {}
54
- },
55
-
56
- createdAt: {
57
- type: Sequelize.DATE,
58
- allowNull: false,
59
- defaultValue: Sequelize.fn("NOW")
60
- },
61
-
62
- updatedAt: {
63
- type: Sequelize.DATE,
64
- allowNull: false,
65
- defaultValue: Sequelize.fn("NOW")
66
- }
67
- });
68
-
69
- await queryInterface.addIndex("transactions", ["userId"]);
70
- await queryInterface.addIndex("transactions", ["paymentId"]);
71
- await queryInterface.addIndex("transactions", ["status"]);
72
- },
73
-
74
- async down(queryInterface) {
75
- await queryInterface.dropTable("transactions");
76
- await queryInterface.sequelize.query(
77
- 'DROP TYPE IF EXISTS "enum_transactions_status";'
78
- );
79
- }
80
- };