diff --git a/packages/bff/prisma/migrations/20231204094144_ste/migration.sql b/packages/bff/prisma/migrations/20231204094144_ste/migration.sql deleted file mode 100644 index 173af0f7..00000000 --- a/packages/bff/prisma/migrations/20231204094144_ste/migration.sql +++ /dev/null @@ -1,34 +0,0 @@ --- CreateTable -CREATE TABLE "scheme_transaction" ( - "id" SERIAL NOT NULL, - "schemeCode" TEXT NOT NULL, - "aadhaarNumber" TEXT NOT NULL, - "aadhaarReferenceNumber" TEXT NOT NULL, - "uniqueBeneficiaryId" TEXT NOT NULL, - "financialYear" TEXT NOT NULL, - "transactionType" TEXT NOT NULL, - "transactionAmount" INTEGER NOT NULL, - "inKindBenefitDetail" TEXT NOT NULL, - "transactionDate" TEXT NOT NULL, - "remarks" TEXT, - "departmentData" JSONB, - "userId" TEXT NOT NULL, - - CONSTRAINT "scheme_transaction_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "transaction_history_table" ( - "id" UUID NOT NULL DEFAULT gen_random_uuid(), - "requestBody" JSONB[], - "containErrors" BOOLEAN NOT NULL DEFAULT false, - "validRecordsSaved" BOOLEAN NOT NULL DEFAULT false, - "errors" JSONB NOT NULL, - "userId" TEXT NOT NULL, - "transactionStartTime" TIMESTAMP(3) NOT NULL, - "transactionEndTime" TIMESTAMP(3) NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "transaction_history_table_pkey" PRIMARY KEY ("id") -); diff --git a/packages/bff/prisma/migrations/20231204115049_ste/migration.sql b/packages/bff/prisma/migrations/20231204115049_ste/migration.sql new file mode 100644 index 00000000..8f05ac1c --- /dev/null +++ b/packages/bff/prisma/migrations/20231204115049_ste/migration.sql @@ -0,0 +1,37 @@ +-- CreateTable +CREATE TABLE "scheme_transaction" ( + "id" SERIAL NOT NULL, + "scheme_code" TEXT NOT NULL, + "aadhaar_number" VARCHAR(12) NOT NULL, + "aadhaar_reference_number" VARCHAR(13) NOT NULL, + "unique_beneficiary_id" VARCHAR(127) NOT NULL, + "financial_year" VARCHAR(15) NOT NULL, + "transaction_type" VARCHAR(127) NOT NULL, + "transaction_amount" INTEGER NOT NULL, + "in_kind_benefit_detail" VARCHAR(127) NOT NULL, + "transaction_date" VARCHAR(15) NOT NULL, + "remarks" TEXT, + "department_data" JSONB, + "user_id" UUID NOT NULL, + + CONSTRAINT "scheme_transaction_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "transaction_history_table" ( + "id" UUID NOT NULL DEFAULT gen_random_uuid(), + "request_body" JSONB[], + "total_records" INTEGER NOT NULL, + "valid_records" INTEGER NOT NULL, + "invalid_records" INTEGER NOT NULL, + "contain_errors" BOOLEAN NOT NULL DEFAULT false, + "valid_records_saved" BOOLEAN NOT NULL DEFAULT false, + "errors" JSONB NOT NULL, + "user_id" UUID NOT NULL, + "transaction_start_time" TIMESTAMP(3) NOT NULL, + "transaction_end_time" TIMESTAMP(3) NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "transaction_history_table_pkey" PRIMARY KEY ("id") +); diff --git a/packages/bff/prisma/schema.prisma b/packages/bff/prisma/schema.prisma index 5327f0ac..97d04fca 100644 --- a/packages/bff/prisma/schema.prisma +++ b/packages/bff/prisma/schema.prisma @@ -125,30 +125,33 @@ model VillageData { } model scheme_transaction { - id Int @id @default(autoincrement()) - schemeCode String - aadhaarNumber String - aadhaarReferenceNumber String - uniqueBeneficiaryId String - financialYear String - transactionType String - transactionAmount Int - inKindBenefitDetail String - transactionDate String - remarks String? - departmentData Json? - userId String + id Int @id @default(autoincrement()) + scheme_code String + aadhaar_number String @db.VarChar(12) + aadhaar_reference_number String @db.VarChar(13) + unique_beneficiary_id String @db.VarChar(127) + financial_year String @db.VarChar(15) + transaction_type String @db.VarChar(127) + transaction_amount Int + in_kind_benefit_detail String @db.VarChar(127) + transaction_date String @db.VarChar(15) + remarks String? + department_data Json? + user_id String @db.Uuid } model transaction_history_table { - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid - requestBody Json[] - containErrors Boolean @default(false) - validRecordsSaved Boolean @default(false) - errors Json - userId String - transactionStartTime DateTime - transactionEndTime DateTime - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt -} \ No newline at end of file + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid + request_body Json[] + total_records Int + valid_records Int + invalid_records Int + contain_errors Boolean @default(false) + valid_records_saved Boolean @default(false) + errors Json + user_id String @db.Uuid + transaction_start_time DateTime + transaction_end_time DateTime + created_at DateTime @default(now()) + updated_at DateTime @updatedAt +} diff --git a/packages/bff/src/common/ste.auth-guard.ts b/packages/bff/src/common/ste.auth-guard.ts index 217bd3b8..896373b8 100644 --- a/packages/bff/src/common/ste.auth-guard.ts +++ b/packages/bff/src/common/ste.auth-guard.ts @@ -56,7 +56,7 @@ export class SteAuthGuard implements CanActivate { console.log('JWT verification error:', err); resolve(false); } else { - if (decoded['roles'][0] !== 'department') { + if (!decoded['roles'].includes('department')) { resolve(false); } request.headers.userId = decoded.sub; diff --git a/packages/bff/src/ste/ste.service.ts b/packages/bff/src/ste/ste.service.ts index a6696625..7946c463 100644 --- a/packages/bff/src/ste/ste.service.ts +++ b/packages/bff/src/ste/ste.service.ts @@ -3,7 +3,6 @@ import { PrismaService } from 'src/prisma/prisma.service'; import { SchemeTransactionEvent } from './dto/scheme.transaction.dto'; import axios from 'axios'; import { SteValidator } from './ste.validator'; -import { map } from 'lodash'; @Injectable() export class SteService { @@ -39,20 +38,6 @@ export class SteService { } } - async transformSchemeTransaction( - schemeTransactions: SchemeTransactionEvent[], - deptUsername: string, - ): Promise { - return await Promise.all( - map(schemeTransactions, async (transaction) => { - return { - userId: deptUsername, - ...transaction, - }; - }), - ); - } - async saveSchemeTransaction( schemetransactions: SchemeTransactionEvent[], userId: string, @@ -68,27 +53,42 @@ export class SteService { let validSchemeTransactionsSaved = false; try { - const records = await this.transformSchemeTransaction( - validSchemeTransactions, - userId, - ); await this.prismaService.scheme_transaction.createMany({ - data: records, + data: validSchemeTransactions.map((entry) => ({ + scheme_code: entry.schemeCode, + aadhaar_number: entry.aadhaarNumber, + aadhaar_reference_number: entry.aadhaarReferenceNumber, + unique_beneficiary_id: entry.uniqueBeneficiaryId, + financial_year: entry.financialYear, + transaction_type: entry.transactionType, + transaction_amount: parseInt(entry.transactionAmount), // validator ensures transactionAmount is always a number + in_kind_benefit_detail: entry.inKindBenefitDetail, + transaction_date: entry.transactionDate, + remarks: entry.remarks, + department_data: entry.departmentData, + user_id: userId, + })), }); validSchemeTransactionsSaved = true; - } catch (error) {} + } catch (error) { + console.log(error.data); + } let transactionHistory; try { transactionHistory = await this.prismaService.transaction_history_table.create({ data: { - requestBody: schemeTransactionEvents, - containErrors: validatedSchemeTransactionEventBody.errorCount !== 0, + request_body: schemeTransactionEvents, + total_records: schemetransactions.length, + valid_records: validSchemeTransactions.length, + invalid_records: validatedSchemeTransactionEventBody.errorCount, + contain_errors: + validatedSchemeTransactionEventBody.errorCount !== 0, errors: validatedSchemeTransactionEventBody.errors, - userId, - validRecordsSaved: validSchemeTransactionsSaved, - transactionStartTime: transactionStartTime, - transactionEndTime: new Date(), + user_id: userId, + valid_records_saved: validSchemeTransactionsSaved, + transaction_start_time: transactionStartTime, + transaction_end_time: new Date(), }, }); } catch (error) { @@ -99,8 +99,9 @@ export class SteService { } return { transactionId: transactionHistory.id, - savedTransactionsCount: - validatedSchemeTransactionEventBody.validSchemeTransactions.length, + savedTransactionsCount: validSchemeTransactionsSaved + ? validatedSchemeTransactionEventBody.validSchemeTransactions.length + : 0, errorTransactionsCount: validatedSchemeTransactionEventBody.errorCount, }; } @@ -124,7 +125,7 @@ export class SteService { async getProgress(userId: string) { const recordCount = await this.prismaService.scheme_transaction.count({ where: { - userId: userId, + user_id: userId, }, }); return { records_saved: recordCount };