Skip to content

Calculating Amount & Stripe APIs #454

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

Merged
merged 1 commit into from Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
ES_HOST=http://dockerhost:9200
DATABASE_URL=postgres://postgres:postgres@dockerhost:5432/postgres
BUSAPI_URL=http://dockerhost:8002/v5
# stripe
STRIPE_SECRET_KEY=
CURRENCY=usd
```

- Values from this file would be automatically used by many `npm` commands.
Expand Down
130 changes: 130 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,110 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
/taas-teams/calculateAmount:
post:
tags:
- Teams
description: |
Calculates total amount for the team.

**Authorization** Any Topcoder user with valid token is allowed. For not logged users Topcoder m2m token with create:taas-teams scope is allowed.
security:
- bearerAuth: []
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/CalculateAmountRequestBody"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/CalculateAmountResponse"
"400":
description: Bad request
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Not authenticated
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"403":
description: Forbidden
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409":
description: Conflict
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/taas-teams/createPayment:
post:
tags:
- Teams
description: |
Calculates total amount for the team.

**Authorization** Any Topcoder user with valid token is allowed. For not logged users Topcoder m2m token with create:taas-teams scope is allowed.
security:
- bearerAuth: []
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/CreatePaymentRequestBody"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/CreatePaymentResponse"
"400":
description: Bad request
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Not authenticated
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"403":
description: Forbidden
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409":
description: Conflict
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/taas-teams/members-suggest/{fragment}:
get:
tags:
Expand Down Expand Up @@ -5589,6 +5693,32 @@ components:
type: string
description: "Optional job title."
example: "Lead Application Developer"
CalculateAmountRequestBody:
properties:
numberOfResources:
type: number
description: "No. of resources required."
rates:
type: number
description: "Weekly rates"
durationWeeks:
type: number
description: "No. of weeks"
CalculateAmountResponse:
properties:
totalAmount:
type: number
description: "Total amount calculated"
CreatePaymentRequestBody:
properties:
totalAmount:
type: number
description: "Total amount charged to user via stripe"
CreatePaymentResponse:
properties:
paymentIntentToken:
type: string
description: " Token required by stripe for completing payment."
SubmitTeamRequestBody:
properties:
teamName:
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"prompt-confirm": "^2.0.4",
"rewire": "^5.0.0",
"sequelize": "^6.3.5",
"stripe": "^8.168.0",
"superagent": "^6.1.0",
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6",
"util": "^0.12.3",
Expand Down
22 changes: 21 additions & 1 deletion src/controllers/TeamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ async function suggestMembers (req, res) {
res.send(await service.suggestMembers(req.authUser, req.params.fragment))
}

/**
*
* @param req the request
* @param res the response
*/
async function calculateAmount(req, res) {
res.send(await service.calculateAmount(req.body));
}

/**
*
* @param req the request
* @param res the response
*/
async function createPayment(req, res) {
res.send(await service.createPayment(req.body.totalAmount));
}

module.exports = {
searchTeams,
getTeam,
Expand All @@ -169,5 +187,7 @@ module.exports = {
roleSearchRequest,
createTeam,
searchSkills,
suggestMembers
suggestMembers,
createPayment,
calculateAmount
}
14 changes: 13 additions & 1 deletion src/routes/TeamRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,17 @@ module.exports = {
auth: 'jwt',
scopes: []
}
}
},
"/taas-teams/calculateAmount": {
post: {
controller: "TeamController",
method: "calculateAmount",
},
},
"/taas-teams/createPayment": {
post: {
controller: "TeamController",
method: "createPayment",
},
}
}
27 changes: 27 additions & 0 deletions src/services/TeamService.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { getAuditM2Muser } = require('../common/helper')
const { matchedSkills, unMatchedSkills } = require('../../scripts/emsi-mapping/esmi-skills-mapping')
const Role = models.Role
const RoleSearchRequest = models.RoleSearchRequest
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

const emailTemplates = _.mapValues(emailTemplateConfig, (template) => {
return {
Expand Down Expand Up @@ -1162,6 +1163,30 @@ suggestMembers.schema = Joi.object().keys({
fragment: Joi.string().required()
}).required()

/**
* Calculates total amount
* @param {Object} body
* @returns {int} totalAmount
*/
async function calculateAmount(body) {
const totalAmount = body.numberOfResources * body.rates * body.durationWeeks;
return { totalAmount };
}

/**
* Creates token for stripe
* @param {int} totalAmount
* @returns {string} paymentIntentToken
*/
async function createPayment(totalAmount) {
const paymentIntent = await stripe.paymentIntents.create({
amount: totalAmount,
currency: process.env.CURRENCY,
});
return { paymentIntentToken: paymentIntent.client_secret };
}


module.exports = {
searchTeams,
getTeam,
Expand All @@ -1180,6 +1205,8 @@ module.exports = {
createRoleSearchRequest,
isExternalMember,
createTeam,
calculateAmount,
createPayment,
searchSkills,
suggestMembers
}