Skip to content

Commit

Permalink
Merge pull request #27 from FlavioAandres/user_login_singup
Browse files Browse the repository at this point in the history
Supporting user sing up and login.
  • Loading branch information
FlavioAandres authored Dec 27, 2020
2 parents 4531eb6 + 80c641a commit 733fd9f
Show file tree
Hide file tree
Showing 26 changed files with 3,110 additions and 290 deletions.
40 changes: 20 additions & 20 deletions API/Functions/BoxFlow/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const { connect: connectToMongo, destroy: detroyMongoConnection, connectToMongoed } = require("../../../shared/database/mongo");
const PaymentRepo = require("../../../shared/database/repos/payment.repo");
const { getUser } = require("../../../shared/database/repos/user.repo");
const _ = require('lodash')
const moment = require('moment')
const {
PHONE_NUMBER
} = process.env

const processMonthlyMetrics = async (userId) => {

Expand Down Expand Up @@ -48,47 +44,51 @@ const processCategoryMetrics = async (userId) => {
return results
}

const processHomeMetrics = async (userId, date) =>{
const result = await PaymentRepo.getAllByDate({userId, date})
const processHomeMetrics = async (userId, date) => {

const result = await PaymentRepo.getAllByDate({ userId, date })
let latestPayments = [], expensivePayments = [], totalByCategory = [], acceptedPayments = [], prepayments = []

//Split types
result.forEach(item=>{
if(item.isAccepted)
result.forEach(item => {
if (item.isAccepted)
acceptedPayments.push(item)
else
else
prepayments.push(item)
})

//prepayments
prepayments = prepayments.slice(0,10)
prepayments = prepayments.slice(0, 10)

//Latest payments
latestPayments = acceptedPayments.slice(0,10)
latestPayments = acceptedPayments.slice(0, 10)

//expensivePayments
expensivePayments = _.orderBy(acceptedPayments, 'amount', ['desc']).slice(0,10)
expensivePayments = _.orderBy(acceptedPayments, 'amount', ['desc']).slice(0, 10)

// Group by category
totalByCategory = _.groupBy(acceptedPayments, 'category')
return {
latestPayments,
expensivePayments,
totalByCategory,
latestPayments,
expensivePayments,
totalByCategory,
prepayments
}
}

//stats endpoint
module.exports.get = async (event, context, callback) => {
let results = [];
const { multiValueQueryStringParameters: queryParams } = event;

const metricType = queryParams && queryParams.metricType ? queryParams.metricType[0] : 'month';
const { query: queryParams, cognitoPoolClaims } = event;
const { sub } = cognitoPoolClaims

const metricType = queryParams && queryParams.metricType ? queryParams.metricType : 'month';
const date = queryParams && queryParams.date ? queryParams.date[0] : moment().subtract(1, 'month').toString()

try {
// This function open the mongo connection
const user = await getUser({ phones: PHONE_NUMBER })
const user = await getUser({ sub })
switch (metricType) {
case 'month':
results = await processMonthlyMetrics(user._id)
Expand All @@ -99,7 +99,6 @@ module.exports.get = async (event, context, callback) => {
case 'home':
results = await processHomeMetrics(user._id, date)
break;

default:
break;
}
Expand All @@ -113,6 +112,7 @@ module.exports.get = async (event, context, callback) => {
message: JSON.stringify(error),
};
}

return {
statusCode: "200",
headers: {
Expand Down
11 changes: 5 additions & 6 deletions API/Functions/DataCredit/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const DataCreditRepo = require("../../../shared/database/repos/datacredito.repo");

const {
PHONE_NUMBER
} = process.env

module.exports.get = async (event, context, callback) => {
const { body } = event;
const { cognitoPoolClaims } = event;
const {
sub
} = cognitoPoolClaims
let results = {};
const date = new Date()

try {
results = await DataCreditRepo.getdataCreditos({ user: { phones: PHONE_NUMBER }, datacredit: { date: { month: (date.getMonth() + 1).toString(), year: date.getFullYear().toString() } } });
results = await DataCreditRepo.getdataCreditos({ user: { sub }, datacredit: { date: { month: (date.getMonth() + 1).toString(), year: date.getFullYear().toString() } } });
} catch (error) {
return {
statusCode: "500",
Expand Down
19 changes: 12 additions & 7 deletions API/Functions/Payments/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const PaymentRepo = require("../../../shared/database/repos/payment.repo");
const { getUser } = require("../../../shared/database/repos/user.repo");
const { destroy: detroyMongoConnection } = require("../../../shared/database/mongo");
const {
PHONE_NUMBER
} = process.env

module.exports.get = async (event, context, callback) => {
let results = {};
const { cognitoPoolClaims } = event
const {
sub
} = cognitoPoolClaims
try {
results = await PaymentRepo.getActive({ phones: PHONE_NUMBER });
results = await PaymentRepo.getActive({ sub });
} catch (error) {
return {
statusCode: "500",
Expand All @@ -28,18 +29,22 @@ module.exports.get = async (event, context, callback) => {
};

module.exports.put = async (event, context, callback) => {
const { body: bodyString } = event
const { body: bodyString, cognitoPoolClaims } = event
const {
id,
description,
category,
hide = false,
accepted = true
} = JSON.parse(bodyString)
} = bodyString

const {
sub
} = cognitoPoolClaims

try {
if (!id || !description || !category) return { statusCode: 400, body: JSON.stringify({ message: 'Bad request' }) }
const user = await getUser({ phones: PHONE_NUMBER })
const user = await getUser({ sub })
const data = await PaymentRepo.updatePayment({
id,
user: user._id,
Expand Down
62 changes: 56 additions & 6 deletions API/Functions/Users/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const UserRepo = require("./../../../shared/database/repos/user.repo");
const { encrypt, decrypt } = require('../../../shared/utils/crypto')
module.exports.getUserInformation = async () => {

module.exports.getUserInformation = async (event) => {
try {
const {
cognitoPoolClaims
} = event

const {
sub
} = cognitoPoolClaims

const result = await UserRepo.getUser(
{
emails: process.env.EMAIL_USERNAME,
sub
},
{ banks: true }
);
Expand Down Expand Up @@ -32,8 +41,8 @@ module.exports.getUserInformation = async () => {

module.exports.addNewCategory = async (event) => {

const body = event.body ? JSON.parse(event.body) : {};

const body = event.body ? event.body : {};
if (!body.label || !body.value)
return {
statusCode: 400,
Expand All @@ -42,10 +51,18 @@ module.exports.addNewCategory = async (event) => {
},
};

const {
cognitoPoolClaims
} = event

const {
sub
} = cognitoPoolClaims

try {
const result = await UserRepo.createCategory(
{
emails: process.env.EMAIL_USERNAME,
sub
},
{ label: body.label, value: body.value }
);
Expand Down Expand Up @@ -77,8 +94,16 @@ module.exports.checkSecretKey = async (event) => {
}
}

const {
cognitoPoolClaims
} = event

const {
sub
} = cognitoPoolClaims

const user = await UserRepo.getUser({
emails: process.env.EMAIL_USERNAME,
sub
})

if (!user.secretKey) return {
Expand All @@ -103,4 +128,29 @@ module.exports.checkSecretKey = async (event) => {
"Access-Control-Allow-Origin": "*",
}
}
}

module.exports.postConfirmation = async (event, context, callback) => {

const { userName, request } = event
const { userAttributes } = request
const { sub, email_verified, phone_number, email } = userAttributes
try {
await UserRepo.create({
name: userName,
email,
sub,
phones: [
phone_number
],
verified: email_verified,
emails: [
email
]
})
callback(null, event)
} catch (error) {
callback(error, event)
}

}
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
## Deploy Client

### Deploy Cognito Pool

#### Install Amplify CLI

``` bash
npm i @aws-amplify/cli -g
```

#### Push Amplify stack

``` bash
amplify push
```

#### Install Client Modules

``` bash
npm install
```

### Update costants.js

``` js
export default {
apiGateway: {
REGION: "us-east-1",
URL: "https://XXXXXXX.execute-api.us-east-1.amazonaws.com/STAGE",
},
}
```


> Note: Log in into your aws account and grab the Conginto Manage pool ARN, you will need this for the serverless config.
## Conection to Mongo Database

#### SRV Way
Expand Down Expand Up @@ -158,10 +194,9 @@ sls deploy --stage [DEV/TEST/PROD]
+ TransferReception: "Bancolombia le informa tecepción de Transferencia"
+ DebitWithdrawal: "Bancolombia le informa Retiro"


## Data Credito

```json
``` json

{
"_id": {
Expand Down Expand Up @@ -190,10 +225,10 @@ sls deploy --stage [DEV/TEST/PROD]
}
```


# Data Credito Scraper DICLAIMER

```
THIS SCRAPER IS A RESEARCH BASED PROJECT, WE DON'T ENCOURAGE THE MISUSE OF THIS TOOL FOR BAD INTENTIONS.
WE ALSO RECOMMEND USERS TO ACQUIRE A PLAN AT www.midatacredito.com TO MAKE USE OF THIS TOOL.
Expand Down
18 changes: 18 additions & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#amplify
amplify/\#current-cloud-backend
amplify/.config/local-*
amplify/logs
amplify/mock-data
amplify/backend/amplify-meta.json
amplify/backend/awscloudformation
amplify/backend/.temp
build/
dist/
node_modules/
aws-exports.js
awsconfiguration.json
amplifyconfiguration.json
amplify-build-config.json
amplify-gradle-config.json
amplifytools.xcconfig
.secret-*
11 changes: 11 additions & 0 deletions client/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files.exclude": {
"amplify/.config": true,
"amplify/**/*-parameters.json": true,
"amplify/**/amplify.state": true,
"amplify/**/transform.conf.json": true,
"amplify/#current-cloud-backend": true,
"amplify/backend/amplify-meta.json": true,
"amplify/backend/awscloudformation": true
}
}
17 changes: 17 additions & 0 deletions client/amplify/.config/project-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"projectName": "client",
"version": "3.0",
"frontend": "javascript",
"javascript": {
"framework": "react",
"config": {
"SourceDir": "src",
"DistributionDir": "build",
"BuildCommand": "npm run-script build",
"StartCommand": "npm run-script start"
}
},
"providers": [
"awscloudformation"
]
}
Loading

0 comments on commit 733fd9f

Please sign in to comment.