Skip to content

Commit

Permalink
refactor: change table col names to snake_case
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv-1001 committed Dec 4, 2023
1 parent 6cd825b commit d2b8fdb
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 89 deletions.
34 changes: 0 additions & 34 deletions packages/bff/prisma/migrations/20231204094144_ste/migration.sql

This file was deleted.

37 changes: 37 additions & 0 deletions packages/bff/prisma/migrations/20231204115049_ste/migration.sql
Original file line number Diff line number Diff line change
@@ -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")
);
51 changes: 27 additions & 24 deletions packages/bff/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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
}
2 changes: 1 addition & 1 deletion packages/bff/src/common/ste.auth-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 31 additions & 30 deletions packages/bff/src/ste/ste.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -39,20 +38,6 @@ export class SteService {
}
}

async transformSchemeTransaction(
schemeTransactions: SchemeTransactionEvent[],
deptUsername: string,
): Promise<any[]> {
return await Promise.all(
map(schemeTransactions, async (transaction) => {
return {
userId: deptUsername,
...transaction,
};
}),
);
}

async saveSchemeTransaction(
schemetransactions: SchemeTransactionEvent[],
userId: string,
Expand All @@ -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) {
Expand All @@ -99,8 +99,9 @@ export class SteService {
}
return {
transactionId: transactionHistory.id,
savedTransactionsCount:
validatedSchemeTransactionEventBody.validSchemeTransactions.length,
savedTransactionsCount: validSchemeTransactionsSaved
? validatedSchemeTransactionEventBody.validSchemeTransactions.length
: 0,
errorTransactionsCount: validatedSchemeTransactionEventBody.errorCount,
};
}
Expand All @@ -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 };
Expand Down

0 comments on commit d2b8fdb

Please sign in to comment.