-
Notifications
You must be signed in to change notification settings - Fork 29
Update Distribution Functionality #19
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
Changes from all commits
2cfcdc2
d811484
79a311d
1db63fa
9607c5e
b412d35
c105e16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { Decimal } from "decimal.js" | ||
| import type { Repository } from "typeorm" | ||
| import type { DistributionEntity } from "./distribution.entity" | ||
| import type { CreateDistributionDto, DistributionResponseDto } from "./distribution.dto" | ||
| import type { CreateDistributionDto, DistributionResponseDto, UpdateDistributionDto } from "./distribution.dto" | ||
| import { DistributionStatus, Network } from "../../../types/enums" | ||
|
|
||
| export class DistributionService { | ||
|
|
@@ -20,6 +21,47 @@ export class DistributionService { | |
| } | ||
| } | ||
|
|
||
| async updateDistribution(id: string, updateData: UpdateDistributionDto): Promise<DistributionResponseDto> { | ||
| try { | ||
| const distribution = await this.distributionRepository.findOne({ where: { id } }) | ||
| if (!distribution) { | ||
| throw new Error("Distribution not found") | ||
| } | ||
|
|
||
| const updatedFields: Partial<DistributionEntity> = { ...updateData } | ||
|
|
||
|
Comment on lines
+24
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent lost updates in concurrent PATCH requests. Line 26 + Line 55-Line 56 currently do a read-modify-write without optimistic/pessimistic concurrency control. Two overlapping updates to the same distribution can overwrite each other unintentionally. Consider protecting this flow with row locking in a transaction or optimistic versioning ( Also applies to: 55-57 🤖 Prompt for AI Agents |
||
| if (updateData.userAddress !== undefined) { | ||
| updatedFields.userAddress = updateData.userAddress.toLowerCase() | ||
| } | ||
| if (updateData.tokenAddress !== undefined) { | ||
| updatedFields.tokenAddress = updateData.tokenAddress.toLowerCase() | ||
| } | ||
| if (updateData.tokenSymbol !== undefined) { | ||
| updatedFields.tokenSymbol = updateData.tokenSymbol.toUpperCase() | ||
| } | ||
|
|
||
| if (updateData.totalAmount || updateData.usdRate) { | ||
| const totalAmount = updateData.totalAmount ?? distribution.totalAmount | ||
| const usdRate = updateData.usdRate ?? distribution.usdRate | ||
| updatedFields.totalUsdAmount = this.calculateTotalUsdAmount(totalAmount, usdRate) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| if (updateData.metadata === null) { | ||
| updatedFields.metadata = null | ||
| } else if (updateData.metadata !== undefined) { | ||
| updatedFields.metadata = this.processMetadata(updateData.metadata) | ||
| } | ||
|
|
||
| Object.assign(distribution, updatedFields) | ||
| const savedDistribution = await this.distributionRepository.save(distribution) | ||
|
DioChuks marked this conversation as resolved.
|
||
|
|
||
| return this.formatDistributionResponse(savedDistribution) | ||
| } catch (error) { | ||
| console.error("Error updating distribution:", error) | ||
| throw error instanceof Error ? error : new Error("Failed to update distribution") | ||
| } | ||
| } | ||
|
|
||
| async listDistributions(limit = 50): Promise<DistributionResponseDto[]> { | ||
| try { | ||
| const distributions = await this.distributionRepository.find({ | ||
|
|
@@ -69,10 +111,9 @@ export class DistributionService { | |
|
|
||
| private calculateTotalUsdAmount(totalAmount: string, usdRate: string): string { | ||
| try { | ||
| const amount = Number.parseFloat(totalAmount) | ||
| const rate = Number.parseFloat(usdRate) | ||
| const totalUsd = amount * rate | ||
| return totalUsd.toString() | ||
| const amount = new Decimal(totalAmount) | ||
| const rate = new Decimal(usdRate) | ||
| return amount.mul(rate).toString() | ||
| } catch (error) { | ||
| console.warn("Error calculating total USD amount:", error) | ||
| return "0" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| import EnhancedRouter from "../../../utils/enhancedRouter" | ||
| import policyMiddleware from "../../../appMiddlewares/policy.middleware" | ||
| import { createDistributionSchema } from "./distribution.validation" | ||
| import { | ||
| createDistributionSchema, | ||
| updateDistributionSchema, | ||
| updateDistributionParamsSchema, | ||
| } from "./distribution.validation" | ||
| import { | ||
| createDistribution, | ||
| updateDistribution, | ||
| listDistributions, | ||
| } from "./distribution.controller" | ||
|
|
||
| const distributionRouter = new EnhancedRouter() | ||
|
|
||
| distributionRouter.get("/", listDistributions) | ||
| distributionRouter.post("/", policyMiddleware(createDistributionSchema), createDistribution) | ||
| distributionRouter.patch( | ||
| "/:id", | ||
| policyMiddleware(updateDistributionParamsSchema, "params"), | ||
| policyMiddleware(updateDistributionSchema), | ||
| updateDistribution, | ||
| ) | ||
|
|
||
| export default distributionRouter.getRouter() |
Uh oh!
There was an error while loading. Please reload this page.