@javalabs/prisma-client 1.0.18 → 1.0.19

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.
Files changed (106) hide show
  1. package/.dockerignore +14 -0
  2. package/Dockerfile +23 -0
  3. package/README.md +269 -269
  4. package/dist/index.d.ts +1 -1
  5. package/dist/prisma.service.d.ts +1 -1
  6. package/dist/scripts/add-uuid-to-table.js +32 -32
  7. package/dist/scripts/data-migration/batch-migrator.js +12 -12
  8. package/dist/scripts/data-migration/data-transformer.js +14 -14
  9. package/dist/scripts/data-migration/dependency-resolver.js +23 -23
  10. package/dist/scripts/data-migration/entity-discovery.js +68 -68
  11. package/dist/scripts/data-migration/foreign-key-manager.js +23 -23
  12. package/dist/scripts/data-migration/migration-tool.js +5 -5
  13. package/dist/scripts/data-migration/schema-utils.js +74 -74
  14. package/dist/scripts/data-migration/typecast-manager.js +4 -4
  15. package/dist/scripts/database-initializer.js +5 -5
  16. package/dist/scripts/drop-database.js +5 -5
  17. package/dist/scripts/fix-data-types.js +53 -53
  18. package/dist/scripts/fix-enum-values.js +34 -34
  19. package/dist/scripts/fix-schema-discrepancies.js +40 -40
  20. package/dist/scripts/fix-table-indexes.js +81 -81
  21. package/dist/scripts/migrate-schema-structure.js +4 -4
  22. package/dist/scripts/migrate-uuid.js +19 -19
  23. package/dist/scripts/post-migration-validator.js +49 -49
  24. package/dist/scripts/pre-migration-validator.js +107 -107
  25. package/dist/scripts/reset-database.js +21 -21
  26. package/dist/scripts/retry-failed-migrations.js +28 -28
  27. package/dist/scripts/run-migration.js +5 -5
  28. package/dist/scripts/schema-sync.js +18 -18
  29. package/dist/scripts/sequence-sync-cli.js +55 -55
  30. package/dist/scripts/sequence-synchronizer.js +20 -20
  31. package/dist/scripts/sync-enum-types.js +30 -30
  32. package/dist/scripts/sync-enum-values.js +52 -52
  33. package/dist/scripts/truncate-database.js +10 -10
  34. package/dist/scripts/verify-migration-setup.js +10 -10
  35. package/dist/tsconfig.tsbuildinfo +1 -1
  36. package/migration-config.json +63 -63
  37. package/migration-config.json.bk +95 -95
  38. package/package.json +44 -44
  39. package/prisma/migrations/add_accepts_partial_payments_to_users.sql +19 -0
  40. package/prisma/migrations/add_amount_received_to_manual_payments.sql +19 -0
  41. package/prisma/migrations/add_commission_fields.sql +33 -0
  42. package/prisma/migrations/add_uuid_to_transactions.sql +13 -13
  43. package/prisma/migrations/complete_partial_payments_migration.sql +53 -0
  44. package/prisma/migrations/create_settlements_table.sql +60 -0
  45. package/prisma/schema.prisma +47 -1
  46. package/src/index.ts +23 -23
  47. package/src/prisma-factory.service.ts +40 -40
  48. package/src/prisma.module.ts +9 -9
  49. package/src/prisma.service.ts +16 -16
  50. package/src/scripts/add-uuid-to-table.ts +138 -138
  51. package/src/scripts/create-tenant-schemas.ts +145 -145
  52. package/src/scripts/data-migration/batch-migrator.ts +248 -248
  53. package/src/scripts/data-migration/data-transformer.ts +426 -426
  54. package/src/scripts/data-migration/db-connector.ts +120 -120
  55. package/src/scripts/data-migration/dependency-resolver.ts +174 -174
  56. package/src/scripts/data-migration/entity-discovery.ts +196 -196
  57. package/src/scripts/data-migration/foreign-key-manager.ts +277 -277
  58. package/src/scripts/data-migration/migration-config.json +63 -63
  59. package/src/scripts/data-migration/migration-tool.ts +509 -509
  60. package/src/scripts/data-migration/schema-utils.ts +248 -248
  61. package/src/scripts/data-migration/tenant-migrator.ts +201 -201
  62. package/src/scripts/data-migration/typecast-manager.ts +193 -193
  63. package/src/scripts/data-migration/types.ts +113 -113
  64. package/src/scripts/database-initializer.ts +49 -49
  65. package/src/scripts/drop-database.ts +104 -104
  66. package/src/scripts/dump-source-db.sh +61 -61
  67. package/src/scripts/encrypt-user-passwords.ts +36 -36
  68. package/src/scripts/error-handler.ts +117 -117
  69. package/src/scripts/fix-data-types.ts +241 -241
  70. package/src/scripts/fix-enum-values.ts +357 -357
  71. package/src/scripts/fix-schema-discrepancies.ts +317 -317
  72. package/src/scripts/fix-table-indexes.ts +601 -601
  73. package/src/scripts/migrate-schema-structure.ts +90 -90
  74. package/src/scripts/migrate-uuid.ts +76 -76
  75. package/src/scripts/post-migration-validator.ts +526 -526
  76. package/src/scripts/pre-migration-validator.ts +610 -610
  77. package/src/scripts/reset-database.ts +263 -263
  78. package/src/scripts/retry-failed-migrations.ts +416 -416
  79. package/src/scripts/run-migration.ts +707 -707
  80. package/src/scripts/schema-sync.ts +128 -128
  81. package/src/scripts/sequence-sync-cli.ts +416 -416
  82. package/src/scripts/sequence-synchronizer.ts +127 -127
  83. package/src/scripts/sync-enum-types.ts +170 -170
  84. package/src/scripts/sync-enum-values.ts +563 -563
  85. package/src/scripts/truncate-database.ts +123 -123
  86. package/src/scripts/verify-migration-setup.ts +135 -135
  87. package/tsconfig.json +17 -17
  88. package/dist/scripts/data-migration/dependency-manager.d.ts +0 -9
  89. package/dist/scripts/data-migration/dependency-manager.js +0 -86
  90. package/dist/scripts/data-migration/dependency-manager.js.map +0 -1
  91. package/dist/scripts/data-migration/migration-config.json +0 -63
  92. package/dist/scripts/data-migration/migration-phases.d.ts +0 -5
  93. package/dist/scripts/data-migration/migration-phases.js +0 -55
  94. package/dist/scripts/data-migration/migration-phases.js.map +0 -1
  95. package/dist/scripts/data-migration/multi-source-migrator.d.ts +0 -17
  96. package/dist/scripts/data-migration/multi-source-migrator.js +0 -130
  97. package/dist/scripts/data-migration/multi-source-migrator.js.map +0 -1
  98. package/dist/scripts/data-migration/phase-generator.d.ts +0 -15
  99. package/dist/scripts/data-migration/phase-generator.js +0 -187
  100. package/dist/scripts/data-migration/phase-generator.js.map +0 -1
  101. package/dist/scripts/data-migration.d.ts +0 -22
  102. package/dist/scripts/data-migration.js +0 -593
  103. package/dist/scripts/data-migration.js.map +0 -1
  104. package/dist/scripts/multi-db-migration.d.ts +0 -1
  105. package/dist/scripts/multi-db-migration.js +0 -55
  106. package/dist/scripts/multi-db-migration.js.map +0 -1
@@ -1,63 +1,63 @@
1
- {
2
- "commonSchema": "public",
3
- "tenantInfo": {
4
- "sourceTable": "users",
5
- "tenantIdColumn": "user_id",
6
- "providerIdColumn": "provider_id"
7
- },
8
- "migrationPriorities": {
9
- "high": ["countries", "providers", "users", "api_keys"],
10
- "medium": ["transactions", "charges", "balances"],
11
- "low": ["invoices", "api_request_logs"]
12
- },
13
- "tables": {
14
- "countries": {
15
- "type": "public",
16
- "idField": "id"
17
- },
18
- "providers": {
19
- "type": "public",
20
- "idField": "provider_id",
21
- "dependencies": ["countries"]
22
- },
23
- "users": {
24
- "type": "public",
25
- "idField": "user_id",
26
- "dependencies": ["providers"]
27
- },
28
- "api_keys": {
29
- "type": "public",
30
- "idField": "id",
31
- "dependencies": ["users"]
32
- },
33
- "transactions": {
34
- "type": "public",
35
- "idField": "id",
36
- "filterColumn": "provider_id",
37
- "dependencies": ["users", "providers"]
38
- },
39
- "charges": {
40
- "type": "public",
41
- "idField": "id",
42
- "filterColumn": "provider_id",
43
- "dependencies": ["transactions"]
44
- },
45
- "invoices": {
46
- "type": "public",
47
- "idField": "id",
48
- "filterColumn": "provider_id",
49
- "dependencies": ["transactions", "charges"]
50
- },
51
- "balances": {
52
- "type": "public",
53
- "idField": "id",
54
- "filterColumn": "provider_id",
55
- "dependencies": ["users"]
56
- },
57
- "api_request_logs": {
58
- "type": "public",
59
- "idField": "id",
60
- "dependencies": ["api_keys"]
61
- }
62
- }
63
- }
1
+ {
2
+ "commonSchema": "public",
3
+ "tenantInfo": {
4
+ "sourceTable": "users",
5
+ "tenantIdColumn": "user_id",
6
+ "providerIdColumn": "provider_id"
7
+ },
8
+ "migrationPriorities": {
9
+ "high": ["countries", "providers", "users", "api_keys"],
10
+ "medium": ["transactions", "charges", "balances"],
11
+ "low": ["invoices", "api_request_logs"]
12
+ },
13
+ "tables": {
14
+ "countries": {
15
+ "type": "public",
16
+ "idField": "id"
17
+ },
18
+ "providers": {
19
+ "type": "public",
20
+ "idField": "provider_id",
21
+ "dependencies": ["countries"]
22
+ },
23
+ "users": {
24
+ "type": "public",
25
+ "idField": "user_id",
26
+ "dependencies": ["providers"]
27
+ },
28
+ "api_keys": {
29
+ "type": "public",
30
+ "idField": "id",
31
+ "dependencies": ["users"]
32
+ },
33
+ "transactions": {
34
+ "type": "public",
35
+ "idField": "id",
36
+ "filterColumn": "provider_id",
37
+ "dependencies": ["users", "providers"]
38
+ },
39
+ "charges": {
40
+ "type": "public",
41
+ "idField": "id",
42
+ "filterColumn": "provider_id",
43
+ "dependencies": ["transactions"]
44
+ },
45
+ "invoices": {
46
+ "type": "public",
47
+ "idField": "id",
48
+ "filterColumn": "provider_id",
49
+ "dependencies": ["transactions", "charges"]
50
+ },
51
+ "balances": {
52
+ "type": "public",
53
+ "idField": "id",
54
+ "filterColumn": "provider_id",
55
+ "dependencies": ["users"]
56
+ },
57
+ "api_request_logs": {
58
+ "type": "public",
59
+ "idField": "id",
60
+ "dependencies": ["api_keys"]
61
+ }
62
+ }
63
+ }
@@ -1,95 +1,95 @@
1
- {
2
- "commonSchema": "public",
3
- "tenantInfo": {
4
- "sourceTable": "api_keys",
5
- "tenantIdColumn": "api_key",
6
- "providerIdColumn": "provider_id"
7
- },
8
- "tables": {
9
- "countries": { "type": "public", "idField": "id" },
10
- "users": { "type": "public", "idField": "user_id" },
11
- "providers": { "type": "public", "idField": "provider_id" },
12
- "banks": { "type": "public", "idField": "id" },
13
- "api_keys": { "type": "public", "idField": "id" },
14
- "transactions": {
15
- "type": "tenant",
16
- "idField": "id",
17
- "filterColumn": "provider_id"
18
- },
19
- "invoices": {
20
- "type": "tenant",
21
- "idField": "id",
22
- "filterColumn": "transaction_id",
23
- "via": "transactions"
24
- },
25
- "balances": {
26
- "type": "tenant",
27
- "idField": "id",
28
- "filterColumn": "user_id",
29
- "via": "users"
30
- },
31
- "charges": {
32
- "type": "tenant",
33
- "idField": "id",
34
- "filterColumn": "reference",
35
- "via": "transactions"
36
- },
37
- "payouts": {
38
- "type": "tenant",
39
- "idField": "id",
40
- "filterColumn": "provider_id"
41
- },
42
- "api_request_logs": {
43
- "type": "tenant",
44
- "idField": "id",
45
- "filterColumn": "api_key",
46
- "via": "api_keys"
47
- },
48
- "credit_requests": {
49
- "type": "tenant",
50
- "idField": "id",
51
- "filterColumn": "user_id",
52
- "via": "users"
53
- },
54
- "customers": {
55
- "type": "tenant",
56
- "idField": "id",
57
- "filterColumn": "user_id",
58
- "via": "users"
59
- },
60
- "daily_logs": {
61
- "type": "tenant",
62
- "idField": "id",
63
- "filterColumn": "transaction_id",
64
- "via": "transactions"
65
- },
66
- "global_user_transactions": {
67
- "type": "tenant",
68
- "idField": "id",
69
- "filterColumn": "provider_id"
70
- },
71
- "notifications": {
72
- "type": "tenant",
73
- "idField": "id",
74
- "filterColumn": "user_id",
75
- "via": "users"
76
- },
77
- "pending_references": {
78
- "type": "tenant",
79
- "idField": "id",
80
- "filterColumn": "provider_id"
81
- },
82
- "toku": {
83
- "type": "tenant",
84
- "idField": "id",
85
- "filterColumn": "transaction_id",
86
- "via": "transactions"
87
- },
88
- "transaction_updates": {
89
- "type": "tenant",
90
- "idField": "id",
91
- "filterColumn": "transaction_id",
92
- "via": "transactions"
93
- }
94
- }
95
- }
1
+ {
2
+ "commonSchema": "public",
3
+ "tenantInfo": {
4
+ "sourceTable": "api_keys",
5
+ "tenantIdColumn": "api_key",
6
+ "providerIdColumn": "provider_id"
7
+ },
8
+ "tables": {
9
+ "countries": { "type": "public", "idField": "id" },
10
+ "users": { "type": "public", "idField": "user_id" },
11
+ "providers": { "type": "public", "idField": "provider_id" },
12
+ "banks": { "type": "public", "idField": "id" },
13
+ "api_keys": { "type": "public", "idField": "id" },
14
+ "transactions": {
15
+ "type": "tenant",
16
+ "idField": "id",
17
+ "filterColumn": "provider_id"
18
+ },
19
+ "invoices": {
20
+ "type": "tenant",
21
+ "idField": "id",
22
+ "filterColumn": "transaction_id",
23
+ "via": "transactions"
24
+ },
25
+ "balances": {
26
+ "type": "tenant",
27
+ "idField": "id",
28
+ "filterColumn": "user_id",
29
+ "via": "users"
30
+ },
31
+ "charges": {
32
+ "type": "tenant",
33
+ "idField": "id",
34
+ "filterColumn": "reference",
35
+ "via": "transactions"
36
+ },
37
+ "payouts": {
38
+ "type": "tenant",
39
+ "idField": "id",
40
+ "filterColumn": "provider_id"
41
+ },
42
+ "api_request_logs": {
43
+ "type": "tenant",
44
+ "idField": "id",
45
+ "filterColumn": "api_key",
46
+ "via": "api_keys"
47
+ },
48
+ "credit_requests": {
49
+ "type": "tenant",
50
+ "idField": "id",
51
+ "filterColumn": "user_id",
52
+ "via": "users"
53
+ },
54
+ "customers": {
55
+ "type": "tenant",
56
+ "idField": "id",
57
+ "filterColumn": "user_id",
58
+ "via": "users"
59
+ },
60
+ "daily_logs": {
61
+ "type": "tenant",
62
+ "idField": "id",
63
+ "filterColumn": "transaction_id",
64
+ "via": "transactions"
65
+ },
66
+ "global_user_transactions": {
67
+ "type": "tenant",
68
+ "idField": "id",
69
+ "filterColumn": "provider_id"
70
+ },
71
+ "notifications": {
72
+ "type": "tenant",
73
+ "idField": "id",
74
+ "filterColumn": "user_id",
75
+ "via": "users"
76
+ },
77
+ "pending_references": {
78
+ "type": "tenant",
79
+ "idField": "id",
80
+ "filterColumn": "provider_id"
81
+ },
82
+ "toku": {
83
+ "type": "tenant",
84
+ "idField": "id",
85
+ "filterColumn": "transaction_id",
86
+ "via": "transactions"
87
+ },
88
+ "transaction_updates": {
89
+ "type": "tenant",
90
+ "idField": "id",
91
+ "filterColumn": "transaction_id",
92
+ "via": "transactions"
93
+ }
94
+ }
95
+ }
package/package.json CHANGED
@@ -1,44 +1,44 @@
1
- {
2
- "name": "@javalabs/prisma-client",
3
- "version": "1.0.18",
4
- "description": "Shared Prisma client for Tupay microservices",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "private": false,
8
- "publishConfig": {
9
- "access": "restricted"
10
- },
11
- "scripts": {
12
- "build": "tsc",
13
- "generate": "prisma generate",
14
- "migrate:dev": "prisma migrate dev",
15
- "migrate:deploy": "prisma migrate deploy",
16
- "migrate:data": "node dist/scripts/run-migration.js migrate",
17
- "migrate:data:force": "node dist/scripts/run-migration.js migrate --force",
18
- "migrate:data:yes": "node dist/scripts/run-migration.js migrate -y",
19
- "postinstall": "npx prisma generate"
20
- },
21
- "prisma": {
22
- "schema": "prisma/schema.prisma"
23
- },
24
- "keywords": [],
25
- "author": "",
26
- "license": "ISC",
27
- "dependencies": {
28
- "@nestjs/common": "^10.0.0",
29
- "@nestjs/core": "^10.0.0",
30
- "@prisma/client": "^6.6.0",
31
- "bcrypt": "^6.0.0",
32
- "commander": "^11.1.0",
33
- "dotenv": "^16.4.7",
34
- "pg": "^8.14.1",
35
- "prisma": "^6.6.0",
36
- "uuid": "^11.1.0"
37
- },
38
- "devDependencies": {
39
- "@types/bcrypt": "^5.0.2",
40
- "@types/node": "^20.3.1",
41
- "@types/pg": "^8.11.11",
42
- "typescript": "^5.2.2"
43
- }
44
- }
1
+ {
2
+ "name": "@javalabs/prisma-client",
3
+ "version": "1.0.19",
4
+ "description": "Shared Prisma client for Tupay microservices",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "private": false,
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "generate": "prisma generate",
14
+ "migrate:dev": "prisma migrate dev",
15
+ "migrate:deploy": "prisma migrate deploy",
16
+ "migrate:data": "node dist/scripts/run-migration.js migrate",
17
+ "migrate:data:force": "node dist/scripts/run-migration.js migrate --force",
18
+ "migrate:data:yes": "node dist/scripts/run-migration.js migrate -y",
19
+ "postinstall": "npx prisma generate"
20
+ },
21
+ "prisma": {
22
+ "schema": "prisma/schema.prisma"
23
+ },
24
+ "keywords": [],
25
+ "author": "",
26
+ "license": "ISC",
27
+ "dependencies": {
28
+ "@nestjs/common": "^10.0.0",
29
+ "@nestjs/core": "^10.0.0",
30
+ "@prisma/client": "^6.17.1",
31
+ "bcrypt": "^6.0.0",
32
+ "commander": "^11.1.0",
33
+ "dotenv": "^16.4.7",
34
+ "pg": "^8.14.1",
35
+ "uuid": "^11.1.0"
36
+ },
37
+ "devDependencies": {
38
+ "@types/bcrypt": "^5.0.2",
39
+ "@types/node": "^20.3.1",
40
+ "@types/pg": "^8.11.11",
41
+ "prisma": "^6.17.1",
42
+ "typescript": "^5.2.2"
43
+ }
44
+ }
@@ -0,0 +1,19 @@
1
+ -- Migration: add_accepts_partial_payments_to_users
2
+ -- Description: Agrega el campo accepts_partial_payments a la tabla users
3
+ -- Date: 2025-10-27
4
+ -- Agregar columna accepts_partial_payments a la tabla users
5
+ ALTER TABLE
6
+ users
7
+ ADD
8
+ COLUMN IF NOT EXISTS accepts_partial_payments BOOLEAN DEFAULT FALSE;
9
+
10
+ -- Agregar comentario a la columna
11
+ COMMENT ON COLUMN users.accepts_partial_payments IS 'Indica si el usuario acepta pagos parciales';
12
+
13
+ -- Actualizar usuarios existentes con valor por defecto
14
+ UPDATE
15
+ users
16
+ SET
17
+ accepts_partial_payments = FALSE
18
+ WHERE
19
+ accepts_partial_payments IS NULL;
@@ -0,0 +1,19 @@
1
+ -- Migration: add_amount_received_to_manual_payments
2
+ -- Description: Agrega el campo amount_received a la tabla manual_payments para almacenar el monto real recibido en pagos parciales
3
+ -- Date: 2025-10-27
4
+ -- Agregar columna amount_received a la tabla manual_payments
5
+ ALTER TABLE
6
+ manual_payments
7
+ ADD
8
+ COLUMN IF NOT EXISTS amount_received DECIMAL(10, 2);
9
+
10
+ -- Agregar comentario a la columna
11
+ COMMENT ON COLUMN manual_payments.amount_received IS 'Monto real recibido en caso de pago parcial';
12
+
13
+ -- Actualizar registros existentes: por defecto, amount_received será igual a amount
14
+ UPDATE
15
+ manual_payments
16
+ SET
17
+ amount_received = amount
18
+ WHERE
19
+ amount_received IS NULL;
@@ -0,0 +1,33 @@
1
+ ALTER TABLE
2
+ users
3
+ ADD
4
+ COLUMN IF NOT EXISTS payin_commission_percent DECIMAL(5, 2) DEFAULT 0,
5
+ ADD
6
+ COLUMN IF NOT EXISTS payin_fixed_fee DECIMAL(10, 2) DEFAULT 0,
7
+ ADD
8
+ COLUMN IF NOT EXISTS payout_fixed_fee DECIMAL(10, 2) DEFAULT 0;
9
+
10
+ ALTER TABLE
11
+ transactions
12
+ ADD
13
+ COLUMN IF NOT EXISTS commission DECIMAL(10, 2) DEFAULT 0,
14
+ ADD
15
+ COLUMN IF NOT EXISTS fixed_fee DECIMAL(10, 2) DEFAULT 0,
16
+ ADD
17
+ COLUMN IF NOT EXISTS total_commission_fee DECIMAL(10, 2) DEFAULT 0,
18
+ ADD
19
+ COLUMN IF NOT EXISTS total_operation DECIMAL(10, 2) DEFAULT 0;
20
+
21
+ COMMENT ON COLUMN users.payin_commission_percent IS 'Percentage commission for payin transactions (0-100)';
22
+
23
+ COMMENT ON COLUMN users.payin_fixed_fee IS 'Fixed fee for payin transactions';
24
+
25
+ COMMENT ON COLUMN users.payout_fixed_fee IS 'Fixed fee for payout transactions';
26
+
27
+ COMMENT ON COLUMN transactions.commission IS 'Commission amount calculated from percentage';
28
+
29
+ COMMENT ON COLUMN transactions.fixed_fee IS 'Fixed fee amount applied to the transaction';
30
+
31
+ COMMENT ON COLUMN transactions.total_commission_fee IS 'Total of commission + fixed_fee';
32
+
33
+ COMMENT ON COLUMN transactions.total_operation IS 'Total amount including transaction amount + total_commission_fee';
@@ -1,14 +1,14 @@
1
- -- Habilitar la extensión uuid-ossp si no está habilitada
2
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
3
-
4
- -- Añadir la columna uuid como nullable
5
- ALTER TABLE transactions ADD COLUMN uuid VARCHAR(255);
6
-
7
- -- Actualizar los registros existentes con UUIDs
8
- UPDATE transactions SET uuid = uuid_generate_v4() WHERE uuid IS NULL;
9
-
10
- -- Crear el índice
11
- CREATE INDEX idx_transactions_uuid ON transactions(uuid);
12
-
13
- -- Añadir la restricción unique
1
+ -- Habilitar la extensión uuid-ossp si no está habilitada
2
+ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
3
+
4
+ -- Añadir la columna uuid como nullable
5
+ ALTER TABLE transactions ADD COLUMN uuid VARCHAR(255);
6
+
7
+ -- Actualizar los registros existentes con UUIDs
8
+ UPDATE transactions SET uuid = uuid_generate_v4() WHERE uuid IS NULL;
9
+
10
+ -- Crear el índice
11
+ CREATE INDEX idx_transactions_uuid ON transactions(uuid);
12
+
13
+ -- Añadir la restricción unique
14
14
  ALTER TABLE transactions ADD CONSTRAINT transactions_uuid_key UNIQUE (uuid);
@@ -0,0 +1,53 @@
1
+ -- ============================================
2
+ -- MIGRACIÓN COMPLETA: SISTEMA DE PAGOS PARCIALES
3
+ -- Fecha: 2025-10-27
4
+ -- Descripción: Agrega soporte para pagos parciales
5
+ -- ============================================
6
+ -- 1. Agregar columna accepts_partial_payments a la tabla users
7
+ -- --------------------------------------------
8
+ ALTER TABLE
9
+ users
10
+ ADD
11
+ COLUMN IF NOT EXISTS accepts_partial_payments BOOLEAN DEFAULT FALSE;
12
+
13
+ -- Agregar comentario descriptivo
14
+ COMMENT ON COLUMN users.accepts_partial_payments IS 'Indica si el usuario acepta pagos parciales';
15
+
16
+ -- Actualizar usuarios existentes con valor por defecto
17
+ UPDATE
18
+ users
19
+ SET
20
+ accepts_partial_payments = FALSE
21
+ WHERE
22
+ accepts_partial_payments IS NULL;
23
+
24
+ -- 2. Agregar columna amount_received a la tabla manual_payments
25
+ -- --------------------------------------------
26
+ ALTER TABLE
27
+ manual_payments
28
+ ADD
29
+ COLUMN IF NOT EXISTS amount_received DECIMAL(10, 2);
30
+
31
+ -- Agregar comentario descriptivo
32
+ COMMENT ON COLUMN manual_payments.amount_received IS 'Monto real recibido en caso de pago parcial';
33
+
34
+ -- Actualizar registros existentes: por defecto, amount_received será igual a amount
35
+ UPDATE
36
+ manual_payments
37
+ SET
38
+ amount_received = amount
39
+ WHERE
40
+ amount_received IS NULL;
41
+
42
+ -- ============================================
43
+ -- VERIFICACIÓN (OPCIONAL)
44
+ -- ============================================
45
+ -- Ejecuta estas consultas para verificar que las columnas fueron creadas:
46
+ -- Verificar columna en users:
47
+ -- SELECT column_name, data_type, column_default
48
+ -- FROM information_schema.columns
49
+ -- WHERE table_name = 'users' AND column_name = 'accepts_partial_payments';
50
+ -- Verificar columna en manual_payments:
51
+ -- SELECT column_name, data_type, column_default
52
+ -- FROM information_schema.columns
53
+ -- WHERE table_name = 'manual_payments' AND column_name = 'amount_received';
@@ -0,0 +1,60 @@
1
+ CREATE TYPE enum_settlements_status AS ENUM ('pending', 'completed', 'cancelled');
2
+
3
+ CREATE TABLE IF NOT EXISTS settlements (
4
+ id SERIAL PRIMARY KEY,
5
+ user_id INTEGER NOT NULL,
6
+ settlement_date TIMESTAMPTZ(6) NOT NULL,
7
+ period_start TIMESTAMPTZ(6) NOT NULL,
8
+ period_end TIMESTAMPTZ(6) NOT NULL,
9
+ payin_count INTEGER DEFAULT 0 NOT NULL,
10
+ payin_total_amount DECIMAL(15, 2) DEFAULT 0 NOT NULL,
11
+ payin_total_commission DECIMAL(15, 2) DEFAULT 0 NOT NULL,
12
+ payin_net_amount DECIMAL(15, 2) DEFAULT 0 NOT NULL,
13
+ payout_count INTEGER DEFAULT 0 NOT NULL,
14
+ payout_total_amount DECIMAL(15, 2) DEFAULT 0 NOT NULL,
15
+ payout_total_commission DECIMAL(15, 2) DEFAULT 0 NOT NULL,
16
+ payout_total_with_commission DECIMAL(15, 2) DEFAULT 0 NOT NULL,
17
+ total_commission DECIMAL(15, 2) DEFAULT 0 NOT NULL,
18
+ balance_before DECIMAL(15, 2) DEFAULT 0 NOT NULL,
19
+ settlement_amount DECIMAL(15, 2) DEFAULT 0 NOT NULL,
20
+ balance_after DECIMAL(15, 2) DEFAULT 0 NOT NULL,
21
+ status enum_settlements_status DEFAULT 'pending' NOT NULL,
22
+ notes TEXT,
23
+ currency VARCHAR(3) DEFAULT 'USD' NOT NULL,
24
+ created_at TIMESTAMPTZ(6) DEFAULT NOW() NOT NULL,
25
+ updated_at TIMESTAMPTZ(6) DEFAULT NOW() NOT NULL,
26
+ CONSTRAINT fk_settlements_user FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
27
+ );
28
+
29
+ CREATE INDEX idx_settlements_user_id ON settlements(user_id);
30
+
31
+ CREATE INDEX idx_settlements_period ON settlements(period_start, period_end);
32
+
33
+ CREATE INDEX idx_settlements_date ON settlements(settlement_date);
34
+
35
+ COMMENT ON TABLE settlements IS 'Stores user settlement records including commission calculations';
36
+
37
+ COMMENT ON COLUMN settlements.payin_count IS 'Number of payin transactions in the period';
38
+
39
+ COMMENT ON COLUMN settlements.payin_total_amount IS 'Total amount of payin transactions';
40
+
41
+ COMMENT ON COLUMN settlements.payin_total_commission IS 'Total commission charged on payin transactions';
42
+
43
+ COMMENT ON COLUMN settlements.payin_net_amount IS 'Net amount received after payin commissions (total - commission)';
44
+
45
+ COMMENT ON COLUMN settlements.payout_count IS 'Number of payout transactions in the period';
46
+
47
+ COMMENT ON COLUMN settlements.payout_total_amount IS 'Total amount of payout transactions';
48
+
49
+ COMMENT ON COLUMN settlements.payout_total_commission IS 'Total commission charged on payout transactions';
50
+
51
+ COMMENT ON COLUMN settlements.payout_total_with_commission IS 'Total payout including commissions (total + commission)';
52
+
53
+ COMMENT ON COLUMN settlements.total_commission IS 'Sum of all commissions (payin + payout)';
54
+
55
+ COMMENT ON COLUMN settlements.balance_before IS 'User balance before settlement';
56
+
57
+ COMMENT ON COLUMN settlements.settlement_amount IS 'Amount settled/withdrawn';
58
+
59
+ COMMENT ON COLUMN settlements.balance_after IS 'User balance after settlement';
60
+