vr-migrations 1.0.39 → 1.0.40

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.
@@ -1,5 +1,7 @@
1
+ // src/database/migrations/XXXXXXXXXXXXXX-create-payments-table.js
1
2
  "use strict";
2
3
 
4
+ /** @type {import('sequelize-cli').Migration} */
3
5
  module.exports = {
4
6
  async up(queryInterface, Sequelize) {
5
7
  await queryInterface.createTable("payments", {
@@ -9,7 +11,6 @@ module.exports = {
9
11
  primaryKey: true,
10
12
  allowNull: false,
11
13
  },
12
-
13
14
  userId: {
14
15
  type: Sequelize.UUID,
15
16
  allowNull: false,
@@ -20,23 +21,26 @@ module.exports = {
20
21
  onUpdate: "CASCADE",
21
22
  onDelete: "RESTRICT",
22
23
  },
23
-
24
24
  devicePaymentPlanId: {
25
25
  type: Sequelize.UUID,
26
- allowNull: false,
26
+ allowNull: true,
27
27
  references: {
28
28
  model: "device_payment_plans",
29
29
  key: "id",
30
30
  },
31
31
  onUpdate: "CASCADE",
32
- onDelete: "RESTRICT",
32
+ onDelete: "SET NULL",
33
33
  },
34
-
35
- transactionId: {
34
+ installmentId: {
36
35
  type: Sequelize.UUID,
37
36
  allowNull: true,
37
+ references: {
38
+ model: "installments",
39
+ key: "id",
40
+ },
41
+ onUpdate: "CASCADE",
42
+ onDelete: "SET NULL",
38
43
  },
39
-
40
44
  idempotencyKeyId: {
41
45
  type: Sequelize.UUID,
42
46
  allowNull: true,
@@ -47,60 +51,92 @@ module.exports = {
47
51
  onUpdate: "CASCADE",
48
52
  onDelete: "SET NULL",
49
53
  },
50
-
51
54
  amount: {
52
- type: Sequelize.FLOAT,
55
+ type: Sequelize.DECIMAL(10, 2),
53
56
  allowNull: false,
54
57
  },
55
-
58
+ currency: {
59
+ type: Sequelize.STRING(3),
60
+ allowNull: false,
61
+ defaultValue: "RWF",
62
+ },
56
63
  provider: {
57
- type: Sequelize.ENUM("mtn_momo", "airtel_money"),
64
+ type: Sequelize.ENUM("MTN_MOMO", "AIRTEL_MONEY", "DPO", "FLUTTERWAVE"),
58
65
  allowNull: false,
59
66
  },
60
-
61
67
  providerReference: {
62
- type: Sequelize.STRING,
68
+ type: Sequelize.STRING(255),
63
69
  allowNull: true,
64
70
  },
65
-
66
71
  status: {
67
- type: Sequelize.ENUM("PENDING", "SUCCESSFUL", "FAILED"),
72
+ type: Sequelize.ENUM("PENDING", "SUCCESSFUL", "FAILED", "REFUNDED"),
68
73
  allowNull: false,
69
74
  defaultValue: "PENDING",
70
75
  },
71
-
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
+ },
72
88
  metadata: {
73
89
  type: Sequelize.JSONB,
74
90
  allowNull: false,
75
91
  defaultValue: {},
76
92
  },
77
-
93
+ settledAt: {
94
+ type: Sequelize.DATE,
95
+ allowNull: true,
96
+ },
78
97
  createdAt: {
79
98
  type: Sequelize.DATE,
80
99
  allowNull: false,
81
- defaultValue: Sequelize.fn("NOW"),
100
+ defaultValue: Sequelize.NOW,
82
101
  },
83
-
84
102
  updatedAt: {
85
103
  type: Sequelize.DATE,
86
104
  allowNull: false,
87
- defaultValue: Sequelize.fn("NOW"),
105
+ defaultValue: Sequelize.NOW,
88
106
  },
89
107
  });
90
108
 
91
- await queryInterface.addIndex("payments", ["userId"]);
92
- await queryInterface.addIndex("payments", ["devicePaymentPlanId"]);
93
- await queryInterface.addIndex("payments", ["status"]);
94
- await queryInterface.addIndex("payments", ["provider"]);
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
+ `);
95
137
  },
96
138
 
97
139
  async down(queryInterface) {
98
140
  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
141
  },
106
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.40",
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,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
- };