|
| 1 | +import { MigrationInterface, QueryRunner } from 'typeorm'; |
| 2 | + |
| 3 | +export class OrgViewChangeFinancialStatusCalc1725368161763 |
| 4 | + implements MigrationInterface |
| 5 | +{ |
| 6 | + name = 'OrgViewChangeFinancialStatusCalc1725368161763'; |
| 7 | + |
| 8 | + public async up(queryRunner: QueryRunner): Promise<void> { |
| 9 | + await queryRunner.query( |
| 10 | + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "schema" = $3`, |
| 11 | + ['VIEW', 'OrganizationView', 'public'], |
| 12 | + ); |
| 13 | + await queryRunner.query(`DROP VIEW "OrganizationView"`); |
| 14 | + await queryRunner.query(`CREATE VIEW "OrganizationView" AS |
| 15 | + SELECT |
| 16 | + "organization".id AS "id", |
| 17 | + "organization".status AS "status", |
| 18 | + "organization".created_on AS "createdOn", |
| 19 | + CASE |
| 20 | + WHEN COUNT(DISTINCT CASE WHEN "organization_financial".status NOT IN ('Completed', 'Pending') AND "organization_financial".status IS NOT NULL THEN 1 END) > 0 |
| 21 | + OR COUNT(DISTINCT CASE WHEN "report".status != 'Completed' AND "report".status IS NOT NULL THEN 1 END) > 0 |
| 22 | + OR COUNT(DISTINCT CASE WHEN "partner".status != 'Completed' AND "partner".status IS NOT NULL THEN 1 END) > 0 |
| 23 | + OR COUNT(DISTINCT CASE WHEN "investor".status != 'Completed' AND "investor".status IS NOT NULL THEN 1 END) > 0 |
| 24 | + THEN 'Not Completed' |
| 25 | + ELSE 'Completed' |
| 26 | + END AS "completionStatus", |
| 27 | + "organization_general".name AS "name", |
| 28 | + "organization_general".alias AS "alias", |
| 29 | + "organization_general".contact_person->>'email' AS "adminEmail", |
| 30 | + COUNT(DISTINCT "user".id) AS "userCount", |
| 31 | + "organization_general".logo AS "logo", |
| 32 | + CASE |
| 33 | + WHEN MAX(GREATEST(COALESCE("organization_financial".updated_on, '1970-01-01'), COALESCE("report".updated_on, '1970-01-01'), COALESCE("partner".updated_on, '1970-01-01'), COALESCE("investor".updated_on, '1970-01-01'))) = '1970-01-01' |
| 34 | + THEN NULL |
| 35 | + ELSE MAX(GREATEST(COALESCE("organization_financial".updated_on, '1970-01-01'), COALESCE("report".updated_on, '1970-01-01'), COALESCE("partner".updated_on, '1970-01-01'), COALESCE("investor".updated_on, '1970-01-01')))::text |
| 36 | + END AS "updatedOn" |
| 37 | + FROM |
| 38 | + "organization" "organization" |
| 39 | + LEFT JOIN "organization_general" "organization_general" ON "organization".organization_general_id = "organization_general".id |
| 40 | + LEFT JOIN "user" "user" ON "user".organization_id = "organization".id |
| 41 | + AND "user".deleted_on IS NULL |
| 42 | + AND "user".ROLE = 'employee' |
| 43 | + AND "user".status != 'pending' |
| 44 | + LEFT JOIN "organization_financial" "organization_financial" ON "organization".id = "organization_financial"."organizationId" |
| 45 | + LEFT JOIN "organization_report" "organization_report" ON "organization".organization_report_id = "organization_report".id |
| 46 | + LEFT JOIN "report" "report" ON "organization_report".id = "report"."organizationReportId" |
| 47 | + LEFT JOIN "partner" "partner" ON "organization_report".id = "partner"."organizationReportId" |
| 48 | + LEFT JOIN "investor" "investor" ON "organization_report".id = "investor"."organizationReportId" |
| 49 | + WHERE |
| 50 | + "organization".status != 'pending' |
| 51 | + GROUP BY |
| 52 | + "organization".id, |
| 53 | + "organization_general".id |
| 54 | + `); |
| 55 | + await queryRunner.query( |
| 56 | + `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)`, |
| 57 | + [ |
| 58 | + 'public', |
| 59 | + 'VIEW', |
| 60 | + 'OrganizationView', |
| 61 | + 'SELECT\n "organization".id AS "id",\n "organization".status AS "status",\n "organization".created_on AS "createdOn",\n \tCASE \n WHEN COUNT(DISTINCT CASE WHEN "organization_financial".status NOT IN (\'Completed\', \'Pending\') AND "organization_financial".status IS NOT NULL THEN 1 END) > 0\n OR COUNT(DISTINCT CASE WHEN "report".status != \'Completed\' AND "report".status IS NOT NULL THEN 1 END) > 0\n OR COUNT(DISTINCT CASE WHEN "partner".status != \'Completed\' AND "partner".status IS NOT NULL THEN 1 END) > 0\n OR COUNT(DISTINCT CASE WHEN "investor".status != \'Completed\' AND "investor".status IS NOT NULL THEN 1 END) > 0\n THEN \'Not Completed\'\n ELSE \'Completed\'\n \tEND AS "completionStatus",\n "organization_general".name AS "name",\n "organization_general".alias AS "alias",\n "organization_general".contact_person->>\'email\' AS "adminEmail",\n COUNT(DISTINCT "user".id) AS "userCount",\n "organization_general".logo AS "logo",\n CASE\n WHEN MAX(GREATEST(COALESCE("organization_financial".updated_on, \'1970-01-01\'), COALESCE("report".updated_on, \'1970-01-01\'), COALESCE("partner".updated_on, \'1970-01-01\'), COALESCE("investor".updated_on, \'1970-01-01\'))) = \'1970-01-01\'\n THEN NULL\n ELSE MAX(GREATEST(COALESCE("organization_financial".updated_on, \'1970-01-01\'), COALESCE("report".updated_on, \'1970-01-01\'), COALESCE("partner".updated_on, \'1970-01-01\'), COALESCE("investor".updated_on, \'1970-01-01\')))::text\n END AS "updatedOn"\n FROM\n "organization" "organization"\n LEFT JOIN "organization_general" "organization_general" ON "organization".organization_general_id = "organization_general".id\n LEFT JOIN "user" "user" ON "user".organization_id = "organization".id\n AND "user".deleted_on IS NULL\n AND "user".ROLE = \'employee\'\n AND "user".status != \'pending\'\n LEFT JOIN "organization_financial" "organization_financial" ON "organization".id = "organization_financial"."organizationId"\n LEFT JOIN "organization_report" "organization_report" ON "organization".organization_report_id = "organization_report".id\n LEFT JOIN "report" "report" ON "organization_report".id = "report"."organizationReportId"\n LEFT JOIN "partner" "partner" ON "organization_report".id = "partner"."organizationReportId"\n LEFT JOIN "investor" "investor" ON "organization_report".id = "investor"."organizationReportId"\n WHERE\n "organization".status != \'pending\'\n GROUP BY\n "organization".id,\n "organization_general".id', |
| 62 | + ], |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public async down(queryRunner: QueryRunner): Promise<void> { |
| 67 | + await queryRunner.query( |
| 68 | + `DELETE FROM "typeorm_metadata" WHERE "type" = $1 AND "name" = $2 AND "schema" = $3`, |
| 69 | + ['VIEW', 'OrganizationView', 'public'], |
| 70 | + ); |
| 71 | + await queryRunner.query(`DROP VIEW "OrganizationView"`); |
| 72 | + await queryRunner.query(`CREATE VIEW "OrganizationView" AS SELECT |
| 73 | + "organization".id AS "id", |
| 74 | + "organization".status AS "status", |
| 75 | + "organization".created_on AS "createdOn", |
| 76 | + CASE |
| 77 | + WHEN COUNT(DISTINCT CASE WHEN "organization_financial".status != 'Completed' AND "organization_financial".status IS NOT NULL THEN 1 END) > 0 |
| 78 | + OR COUNT(DISTINCT CASE WHEN "report".status != 'Completed' AND "report".status IS NOT NULL THEN 1 END) > 0 |
| 79 | + OR COUNT(DISTINCT CASE WHEN "partner".status != 'Completed' AND "partner".status IS NOT NULL THEN 1 END) > 0 |
| 80 | + OR COUNT(DISTINCT CASE WHEN "investor".status != 'Completed' AND "investor".status IS NOT NULL THEN 1 END) > 0 |
| 81 | + THEN 'Not Completed' |
| 82 | + ELSE 'Completed' |
| 83 | + END AS "completionStatus", |
| 84 | + "organization_general".name AS "name", |
| 85 | + "organization_general".alias AS "alias", |
| 86 | + "organization_general".contact_person->>'email' AS "adminEmail", |
| 87 | + COUNT(DISTINCT "user".id) AS "userCount", |
| 88 | + "organization_general".logo AS "logo", |
| 89 | + CASE |
| 90 | + WHEN MAX(GREATEST(COALESCE("organization_financial".updated_on, '1970-01-01'), COALESCE("report".updated_on, '1970-01-01'), COALESCE("partner".updated_on, '1970-01-01'), COALESCE("investor".updated_on, '1970-01-01'))) = '1970-01-01' |
| 91 | + THEN NULL |
| 92 | + ELSE MAX(GREATEST(COALESCE("organization_financial".updated_on, '1970-01-01'), COALESCE("report".updated_on, '1970-01-01'), COALESCE("partner".updated_on, '1970-01-01'), COALESCE("investor".updated_on, '1970-01-01')))::text |
| 93 | + END AS "updatedOn" |
| 94 | + FROM |
| 95 | + "organization" "organization" |
| 96 | + LEFT JOIN "organization_general" "organization_general" ON "organization".organization_general_id = "organization_general".id |
| 97 | + LEFT JOIN "user" "user" ON "user".organization_id = "organization".id |
| 98 | + AND "user".deleted_on IS NULL |
| 99 | + AND "user".ROLE = 'employee' |
| 100 | + AND "user".status != 'pending' |
| 101 | + LEFT JOIN "organization_financial" "organization_financial" ON "organization".id = "organization_financial"."organizationId" |
| 102 | + LEFT JOIN "organization_report" "organization_report" ON "organization".organization_report_id = "organization_report".id |
| 103 | + LEFT JOIN "report" "report" ON "organization_report".id = "report"."organizationReportId" |
| 104 | + LEFT JOIN "partner" "partner" ON "organization_report".id = "partner"."organizationReportId" |
| 105 | + LEFT JOIN "investor" "investor" ON "organization_report".id = "investor"."organizationReportId" |
| 106 | + WHERE |
| 107 | + "organization".status != 'pending' |
| 108 | + GROUP BY |
| 109 | + "organization".id, |
| 110 | + "organization_general".id`); |
| 111 | + await queryRunner.query( |
| 112 | + `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value") VALUES (DEFAULT, $1, DEFAULT, $2, $3, $4)`, |
| 113 | + [ |
| 114 | + 'public', |
| 115 | + 'VIEW', |
| 116 | + 'OrganizationView', |
| 117 | + 'SELECT\n "organization".id AS "id",\n "organization".status AS "status",\n "organization".created_on AS "createdOn",\n \tCASE \n WHEN COUNT(DISTINCT CASE WHEN "organization_financial".status != \'Completed\' AND "organization_financial".status IS NOT NULL THEN 1 END) > 0\n OR COUNT(DISTINCT CASE WHEN "report".status != \'Completed\' AND "report".status IS NOT NULL THEN 1 END) > 0\n OR COUNT(DISTINCT CASE WHEN "partner".status != \'Completed\' AND "partner".status IS NOT NULL THEN 1 END) > 0\n OR COUNT(DISTINCT CASE WHEN "investor".status != \'Completed\' AND "investor".status IS NOT NULL THEN 1 END) > 0\n THEN \'Not Completed\'\n ELSE \'Completed\'\n \tEND AS "completionStatus",\n "organization_general".name AS "name",\n "organization_general".alias AS "alias",\n "organization_general".contact_person->>\'email\' AS "adminEmail",\n COUNT(DISTINCT "user".id) AS "userCount",\n "organization_general".logo AS "logo",\n CASE\n WHEN MAX(GREATEST(COALESCE("organization_financial".updated_on, \'1970-01-01\'), COALESCE("report".updated_on, \'1970-01-01\'), COALESCE("partner".updated_on, \'1970-01-01\'), COALESCE("investor".updated_on, \'1970-01-01\'))) = \'1970-01-01\'\n THEN NULL\n ELSE MAX(GREATEST(COALESCE("organization_financial".updated_on, \'1970-01-01\'), COALESCE("report".updated_on, \'1970-01-01\'), COALESCE("partner".updated_on, \'1970-01-01\'), COALESCE("investor".updated_on, \'1970-01-01\')))::text\n END AS "updatedOn"\n FROM\n "organization" "organization"\n LEFT JOIN "organization_general" "organization_general" ON "organization".organization_general_id = "organization_general".id\n LEFT JOIN "user" "user" ON "user".organization_id = "organization".id\n AND "user".deleted_on IS NULL\n AND "user".ROLE = \'employee\'\n AND "user".status != \'pending\'\n LEFT JOIN "organization_financial" "organization_financial" ON "organization".id = "organization_financial"."organizationId"\n LEFT JOIN "organization_report" "organization_report" ON "organization".organization_report_id = "organization_report".id\n LEFT JOIN "report" "report" ON "organization_report".id = "report"."organizationReportId"\n LEFT JOIN "partner" "partner" ON "organization_report".id = "partner"."organizationReportId"\n LEFT JOIN "investor" "investor" ON "organization_report".id = "investor"."organizationReportId"\n WHERE\n "organization".status != \'pending\'\n GROUP BY\n "organization".id,\n "organization_general".id', |
| 118 | + ], |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments