Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apis init #3

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e9ee426
apis init
SanchitUke Sep 22, 2023
723f1f4
response objects
SanchitEsMagico Sep 22, 2023
1532958
swagger module implementation
SanchitEsMagico Sep 22, 2023
bf22ee3
review changes and docker
SanchitEsMagico Oct 3, 2023
799953c
enduser to consumer
SanchitEsMagico Oct 3, 2023
42eed9a
readme updated
SanchitEsMagico Oct 11, 2023
1087ef9
added services diagram
SanchitEsMagico Oct 13, 2023
d07a34c
changed userId to uuid
SanchitEsMagico Nov 9, 2023
6be1526
minor debugging
SanchitEsMagico Nov 9, 2023
77c8992
Added seed data
kh4l1d64 Nov 14, 2023
43232cd
logger and error handling
SanchitEsMagico Nov 14, 2023
19e7114
request type fix
SanchitEsMagico Nov 14, 2023
6a2da72
error fixes
SanchitEsMagico Nov 16, 2023
bba68cd
utils change
SanchitEsMagico Nov 20, 2023
0e8be47
Error message
SanchitEsMagico Nov 27, 2023
39a0789
get all user credits
SanchitEsMagico Nov 28, 2023
824ce71
settlement update
SanchitEsMagico Nov 28, 2023
bd10c0f
seed update
SanchitEsMagico Nov 28, 2023
5e79c39
seed update
SanchitEsMagico Dec 1, 2023
bd7f7f8
refund credits for failed purchase
SanchitEsMagico Dec 7, 2023
2bb9589
seed update
SanchitEsMagico Dec 12, 2023
a9323b3
Updating the docker file for deployment
Dec 13, 2023
d9e5135
Adding final changes for deployment
Dec 15, 2023
b34ebb0
Added logs and updated seed data
kh4l1d64 Apr 17, 2024
335d9fd
Delete .env
kh4l1d64 Apr 17, 2024
743b9f3
Update env-sample
kh4l1d64 Apr 17, 2024
87baba7
Added wallet delete API
kh4l1d64 Apr 21, 2024
8c5bcd1
Creating views for data visualization.
VamshiBatta07 Jul 17, 2024
2588164
Merge pull request #4 from COMPASS-DPG/feature/create-views
VamshiBatta07 Jul 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
Expand Down
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- A unique constraint covering the columns `[userId]` on the table `wallets` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "wallets_userId_key" ON "wallets"("userId");
12 changes: 12 additions & 0 deletions prisma/migrations/20230921071650_transaction_type/migration.sql
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:

- Added the required column `type` to the `transactions` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "TransactionType" AS ENUM ('purchase', 'creditRequest', 'settlement');

-- AlterTable
ALTER TABLE "transactions" ADD COLUMN "type" "TransactionType" NOT NULL,
ALTER COLUMN "description" DROP NOT NULL;
12 changes: 10 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ enum WalletStatus {
frozen
}

enum TransactionType {
purchase
creditRequest
settlement
}
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved


model wallets {
walletId Int @id @default(autoincrement())
userId Int
userId Int @unique
type WalletType
status WalletStatus
credits Int
Expand All @@ -39,7 +46,8 @@ model transactions {
fromId Int
toId Int
credits Int
description String
type TransactionType
description String?
createdAt DateTime @default(now())
from wallets @relation("FromTransaction", fields:[fromId], references:[walletId])
to wallets @relation("ToTransaction", fields:[toId], references:[walletId])
Expand Down
23 changes: 23 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PrismaClient, TransactionType, WalletStatus, WalletType } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
const wallet = await prisma.transactions.create({
data: {
credits: 15,
fromId: 6,
toId: 10,
type: TransactionType.settlement,

}
})
console.log({ wallet })
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
112 changes: 110 additions & 2 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,112 @@
import { Controller } from '@nestjs/common';
import { Body, Controller, Get, Param, ParseIntPipe, Post, UnauthorizedException } from '@nestjs/common';
import { AdminService } from './admin.service';
import { TransactionService } from 'src/transactions/transactions.service';
import { UserService } from 'src/user/user.service';
import { TransactionType, WalletType } from '@prisma/client';
import { ProviderService } from 'src/provider/provider.service';

@Controller('admin')
export class AdminController {}
export class AdminController {
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved
constructor(
private transactionService: TransactionService,
private userService: UserService,
private adminService: AdminService,
private providerService: ProviderService
) {}

@Get("/:adminId/transactions/users")
// get all transactions of all users
async getAllUsersTransactions(
@Param("adminId", ParseIntPipe) adminId: number
) {
// check admin
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved
await this.adminService.getAdminWallet(adminId);

// fetch transactions
const transactions = await this.transactionService.fetchAllUsersTransactions();
return transactions;
}

@Get("/:adminId/transactions/users/:userId")
// get all transactions of a particular user
async getUserTransactions(
@Param("adminId", ParseIntPipe) adminId: number,
@Param("userId", ParseIntPipe) userId: number
) {
// check admin
await this.adminService.getAdminWallet(adminId);

// check user
await this.userService.getUserWallet(userId);

// fetch transactions
const transactions = await this.transactionService.fetchTransactionsOfOneSystemActor(userId);
return transactions;
}

@Get("/:adminId/transactions/providers")
// get all transactions between all providers and admins
async getAllAdminProvidersTransactions(
@Param("adminId", ParseIntPipe) adminId: number
) {
// check admin
await this.adminService.getAdminWallet(adminId);

// fetch transactions
const transactions = await this.transactionService.fetchAllAdminProviderTransactions();
return transactions;
}

@Get("/:adminId/transactions/providers/:providerId")
// get all transactions of a particular provider
async getProviderTransactions(
@Param("adminId", ParseIntPipe) adminId: number,
@Param("providerId", ParseIntPipe) providerId: number
) {
// check admin
await this.adminService.getAdminWallet(adminId);
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved

// check provider
await this.providerService.getProviderWallet(providerId);

// fetch transactions
const transactions = await this.transactionService.fetchTransactionsOfOneSystemActor(providerId);
return transactions;
}

@Post("/:adminId/add-credits")
// add credits to a user's wallet
async addCredits(
@Param("adminId", ParseIntPipe) adminId: number,
@Body("userId") userId: number,
@Body("credits") credits: number
) {
// check admin
const adminWallet = await this.adminService.getAdminWallet(adminId);

// update wallet
const userWallet = await this.userService.addCreditsToUser(userId, credits);

// create transaction
await this.transactionService.createTransaction(credits, adminWallet.walletId, userWallet.walletId, TransactionType.creditRequest);
return userWallet.credits;
}

@Post("/:adminId/reduce-credits")
// reduce credits from a user's wallet
async reduceCredits(
@Param("adminId", ParseIntPipe) adminId: number,
@Body("userId") userId: number,
@Body("credits") credits: number
) {
// check admin
const adminWallet = await this.adminService.getAdminWallet(adminId);

// update wallet
const userWallet = await this.userService.reduceUserCredits(userId, credits);

// create transaction
await this.transactionService.createTransaction(credits, userWallet.walletId, adminWallet.walletId, TransactionType.creditRequest);
return userWallet.credits;
}
}
6 changes: 5 additions & 1 deletion src/admin/admin.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Module } from '@nestjs/common';
import { AdminController } from './admin.controller';
import { AdminService } from './admin.service';
import { TransactionService } from 'src/transactions/transactions.service';
import { WalletService } from 'src/wallet/wallet.service';
import { UserService } from 'src/user/user.service';
import { ProviderService } from 'src/provider/provider.service';

@Module({
controllers: [AdminController],
providers: [AdminService]
providers: [AdminService, TransactionService, WalletService, UserService, ProviderService]
})
export class AdminModule {}
25 changes: 23 additions & 2 deletions src/admin/admin.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
import { WalletType } from '@prisma/client';
import { prisma } from 'src/main';
import { WalletService } from 'src/wallet/wallet.service';

@Injectable()
export class AdminService {}
export class AdminService {
constructor(
private walletService: WalletService,
) {}

async getAdminWallet(adminId: number) {
// get admin wallet
const adminWallet = await this.walletService.fetchWallet(adminId);
if(adminWallet == null) {
throw new NotFoundException;
}

// check admin
if(adminWallet.type != WalletType.admin) {
throw new UnauthorizedException;
}
return adminWallet;
}
}
13 changes: 12 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { PrismaClient } from '@prisma/client';

export const prisma = new PrismaClient();

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
bootstrap()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
26 changes: 23 additions & 3 deletions src/provider/provider.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import { Controller } from '@nestjs/common';
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
import { TransactionService } from 'src/transactions/transactions.service';
import { ProviderService } from './provider.service';

@Controller('provider')
export class ProviderController {}
@Controller('providers')
export class ProviderController {
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved
constructor(
private transactionService: TransactionService,
private providerService: ProviderService
) {}

@Get("/:providerId/transactions")
// get all transactions of a particular provider
async getProviderTransactions(
@Param("providerId", ParseIntPipe) providerId: number,
) {
// check provider
await this.providerService.getProviderWallet(providerId);

// fetch transactions
const transactions = await this.transactionService.fetchTransactionsOfOneSystemActor(providerId);
return transactions;
}
}
4 changes: 3 additions & 1 deletion src/provider/provider.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { ProviderController } from './provider.controller';
import { ProviderService } from './provider.service';
import { TransactionService } from 'src/transactions/transactions.service';
import { WalletService } from 'src/wallet/wallet.service';

@Module({
controllers: [ProviderController],
providers: [ProviderService]
providers: [ProviderService, TransactionService, WalletService]
})
export class ProviderModule {}
35 changes: 33 additions & 2 deletions src/provider/provider.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { WalletType } from '@prisma/client';
import { WalletService } from 'src/wallet/wallet.service';

@Injectable()
export class ProviderService {}
export class ProviderService {
constructor(
private walletService: WalletService,
) {}

async getProviderWallet(providerId: number) {
// get provider wallet
const providerWallet = await this.walletService.fetchWallet(providerId)
if(providerWallet == null) {
throw new NotFoundException;
}

// check provider
if(providerWallet.type != WalletType.provider) {
throw new BadRequestException;
}
return providerWallet;
}

async addCreditsToProvider(providerId: number, credits: number) {

// check provider
let providerWallet = await this.getProviderWallet(providerId)
SanchitEsMagico marked this conversation as resolved.
Show resolved Hide resolved

// update provider wallet
providerWallet = await this.walletService.updateWalletCredits(providerId, providerWallet.credits + credits);
return providerWallet;
}

}
Loading