-
Notifications
You must be signed in to change notification settings - Fork 1
[POC] Kickstart Investment Flow #567
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
Open
briannval
wants to merge
14
commits into
dev
Choose a base branch
from
kickstart-investment-service
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aae8633
Teams + Registrations Modifications
briannval 7e845a9
Formatting with eslint
autofix-ci[bot] 0eda13a
Init investments service
briannval 65aa945
Initial impl of invest endpoint
briannval c3bdf92
Fix typo
briannval 1c55232
Initial implementation of 2 other endpoints
briannval 81a1d0f
Resolve import errors
briannval 2d64d3a
Update code
briannval 579dd79
Use update DB custom
briannval 6a139c6
Modify condition expression
briannval 575b032
Formatting with eslint
autofix-ci[bot] 06302af
Merge branch 'dev' into kickstart-investment-service
briannval 978f061
Try to fix issues
briannval 1f5779b
Formatting with eslint
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| import { USER_REGISTRATIONS_TABLE, INVESTMENTS_TABLE, TEAMS_TABLE } from "../../constants/tables"; | ||
| import db from "../../lib/db"; | ||
| import helpers from "../../lib/handlerHelpers"; | ||
| import { v4 as uuidv4 } from "uuid"; | ||
|
|
||
| // WIP | ||
| export const invest = async (event, ctx, callback) => { | ||
| /* | ||
| Responsible for: | ||
| - Decrementing the balance of the investor | ||
| - Incrementing the balance of the team | ||
| - Updating the DB with transaction + comments | ||
| */ | ||
|
|
||
| const data = JSON.parse(event.body); | ||
|
|
||
| helpers.checkPayloadProps(data, { | ||
| investorId: { | ||
| required: true, | ||
| type: "string" | ||
| }, | ||
| teamId: { | ||
| required: true, | ||
| type: "string" | ||
| }, | ||
| amount: { | ||
| required: true, | ||
| type: "number" | ||
| }, | ||
| comment: { | ||
| required: true, | ||
| type: "string" | ||
| } | ||
| }); | ||
|
|
||
| const investor = await db.getOne(data.investorId, USER_REGISTRATIONS_TABLE, { | ||
| "eventID;year": "kickstart;2025" // hardcoded | ||
| }); | ||
|
|
||
| const team = await db.getOne(data.teamId, TEAMS_TABLE, { | ||
| "eventID;year": "kickstart;2025" // hardcoded | ||
| }); | ||
|
|
||
| // only allow valid investors | ||
| if (!investor) { | ||
| return helpers.createResponse(400, { | ||
| message: "Investor not found or not registered for event" | ||
| }); | ||
| } | ||
|
|
||
| // only allow valid teams | ||
| if (!team) { | ||
| return helpers.createResponse(400, { | ||
| message: "Team not found for event" | ||
| }); | ||
| } | ||
|
|
||
| // investor cannot invest in their own team | ||
| if (investor.teamId === team.id) { | ||
| return helpers.createResponse(400, { | ||
| message: "Investor cannot invest in their own team" | ||
| }); | ||
| } | ||
|
|
||
| // investor cannot invest more than their remaining balance | ||
| if (data.amount > investor.balance) { | ||
| return helpers.createResponse(400, { | ||
| message: "Investor does not have enough balance" | ||
| }); | ||
| } | ||
|
|
||
| // 1. update investor balance | ||
| const updateInvestorPromise = db.updateDBCustom({ | ||
| TableName: USER_REGISTRATIONS_TABLE + (process.env.ENVIRONMENT || ""), | ||
| Key: { | ||
| id: data.investorId, | ||
| "eventID;year": "kickstart;2025" | ||
| }, | ||
| UpdateExpression: "SET balance = :newBalance", | ||
| ExpressionAttributeValues: { | ||
| ":newBalance": investor.balance - data.amount, | ||
| }, | ||
| ConditionExpression: "attribute_exists(id) and attribute_exists(eventID;year)", | ||
| ReturnValues: "UPDATED_NEW", | ||
| }); | ||
|
|
||
| // 2. update team funding | ||
| const updateTeamPromise = db.updateDBCustom({ | ||
| TableName: TEAMS_TABLE + (process.env.ENVIRONMENT || ""), | ||
| Key: { | ||
| id: data.teamId, | ||
| "eventID;year": "kickstart;2025" | ||
| }, | ||
| UpdateExpression: "SET funding = :newFunding", | ||
| ExpressionAttributeValues: { | ||
| ":newFunding": team.funding + data.amount, | ||
| }, | ||
| ConditionExpression: "attribute_exists(id) and attribute_exists(eventID;year)", | ||
| ReturnValues: "UPDATED_NEW", | ||
| }); | ||
|
|
||
| // 3. create investment | ||
| const createInvestmentPromise = db.create({ | ||
| id: uuidv4(), // partition key | ||
| ["eventID;year"]: "kickstart;2025", // sort key | ||
| investorId: data.investorId, | ||
| investorName: investor.fname, | ||
| teamId: data.teamId, | ||
| amount: data.amount, | ||
| comment: data.comment, | ||
| }, INVESTMENTS_TABLE); | ||
|
|
||
| await Promise.all([updateInvestorPromise, updateTeamPromise, createInvestmentPromise]); | ||
|
|
||
| return helpers.createResponse(200, { | ||
| message: "Investment successful" | ||
| }); | ||
| }; | ||
|
|
||
| // WIP | ||
| export const userStatus = async (event, ctx, callback) => { | ||
| /* | ||
| Responsible for: | ||
| - Fetching user current balance | ||
| - Fetching user's stake in other teams | ||
| */ | ||
|
|
||
| const data = JSON.parse(event.body); | ||
|
|
||
| helpers.checkPayloadProps(data, { | ||
| userId: { | ||
| required: true, | ||
| type: "string" | ||
| } | ||
| }); | ||
|
|
||
| const user = await db.getOne(data.userId, USER_REGISTRATIONS_TABLE, { | ||
| "eventID;year": "kickstart;2025" | ||
| }); | ||
|
|
||
| if (!user) { | ||
| return helpers.createResponse(400, { | ||
| message: "User not found or not registered for event" | ||
| }); | ||
| } | ||
|
|
||
| const userInvestments = await db.scan(INVESTMENTS_TABLE, { | ||
| FilterExpression: "#investorId = :investorId", | ||
| ExpressionAttributeNames: { | ||
| "#investorId": "investorId" | ||
| }, | ||
| ExpressionAttributeValues: { | ||
| ":investorId": data.userId | ||
| } | ||
| }); | ||
|
|
||
| // Aggregate user's stakes per team | ||
| const stakes = userInvestments.reduce((accumulatedStakes, investment) => { | ||
| if (!accumulatedStakes[investment.teamId]) { | ||
| accumulatedStakes[investment.teamId] = 0; | ||
| } | ||
|
|
||
| // Add the investment amount to the team's stake | ||
| accumulatedStakes[investment.teamId] += investment.amount; | ||
| return accumulatedStakes; | ||
| }, {}); | ||
|
|
||
| return helpers.createResponse(200, { | ||
| balance: user.balance, | ||
| stakes | ||
| }); | ||
| }; | ||
|
|
||
| // WIP | ||
| export const teamStatus = async (event, ctx, callback) => { | ||
| /* | ||
| Responsible for: | ||
| - Fetching team's current funding | ||
| - Fetching all individual investments with comments | ||
| */ | ||
|
|
||
| const data = JSON.parse(event.body); | ||
|
|
||
| helpers.checkPayloadProps(data, { | ||
| teamId: { | ||
| required: true, | ||
| type: "string" | ||
| } | ||
| }); | ||
|
|
||
| const team = await db.getOne(data.teamId, TEAMS_TABLE, { | ||
| "eventID;year": "kickstart;2025" | ||
| }); | ||
|
|
||
| if (!team) { | ||
| return helpers.createResponse(400, { | ||
| message: "Team not found for event" | ||
| }); | ||
| } | ||
|
|
||
| // Scan all investments made into this team | ||
| const teamInvestments = await db.scan(INVESTMENTS_TABLE, { | ||
| FilterExpression: "#teamId = :teamId", | ||
| ExpressionAttributeNames: { | ||
| "#teamId": "teamId" | ||
| }, | ||
| ExpressionAttributeValues: { | ||
| ":teamId": data.teamId | ||
| } | ||
| }); | ||
|
|
||
| return helpers.createResponse(200, { | ||
| funding: team.funding, | ||
| investments: teamInvestments // each entry includes comment, investorId, investorName, amount | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's confirm with gautham before we go ahead with this