Skip to content

Commit

Permalink
Added a keepers cloud func responsible for triggering expiration, fin…
Browse files Browse the repository at this point in the history
…alization and withdraw process. Added supportive api endpoints for getting all vouchers and checking for voucher payments
  • Loading branch information
VladislavIvanov committed Sep 30, 2020
1 parent 738646f commit c9da81d
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "boson-protocol-prototype-test"
}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
package-lock.json
.DS_Store
yarn.lock
/tests
/tests

.idea
7 changes: 7 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}
16 changes: 12 additions & 4 deletions src/api/controllers/payment-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class UserController {

static async getPaymentActors(req, res, next) {
const objectId = req.params.voucherID


let actors = {
buyer: 0,
Expand All @@ -32,7 +32,7 @@ class UserController {

} catch (error) {
console.error(error)
return next(new APIError(400, `Get payment actors for voucher id: ${userVoucher._tokenIdVoucher} could not be completed.`))
return next(new APIError(400, `Get payment actors for voucher id: ${ userVoucher._tokenIdVoucher } could not be completed.`))
}

res.status(200).send({ actors });
Expand All @@ -51,12 +51,20 @@ class UserController {

} catch (error) {
console.error(error)
return next(new APIError(400, `Create payment operation for voucher id: ${events[0]._tokenIdVoucher} could not be completed.`))
return next(new APIError(400, `Create payment operation for voucher id: ${ events[0]._tokenIdVoucher } could not be completed.`))
}

res.status(200).send({ updated: true });
}

static async getPaymentsByVoucherID(req, res, next) {
const tokenIdVoucher = req.params.tokenIdVoucher;

const payments = await mongooseService.getPaymentsByVoucherID(tokenIdVoucher);

res.status(200).send({ payments })
}

}

module.exports = UserController;
module.exports = UserController;
18 changes: 12 additions & 6 deletions src/api/controllers/user-vouchers-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const voucherUtils = require('../../utils/voucherUtils')
const statuses = require('../../utils/userVoucherStatus');

class UserVoucherController {

static async getMyVouchers(req, res, next) {
const voucherData = []
const address = res.locals.address;

const userVouchersDocuments = await mongooseService.getMyVouchers(address)

const promises = []
Expand Down Expand Up @@ -38,7 +38,7 @@ class UserVoucherController {

static async getVoucherDetails(req, res, next) {
const myVoucherID = req.params.voucherID

const myVoucherDocument = await mongooseService.getMyVoucherByID(myVoucherID)
const voucherDetailsDocument = await mongooseService.getVoucher(myVoucherDocument.voucherID)

Expand Down Expand Up @@ -73,12 +73,12 @@ class UserVoucherController {
}

res.status(200).send({ voucher })
}
}

static async updateMyVoucher(req, res, next) {
const userVoucherID = res.locals.userVoucher.id;
const status = req.body.status

try {
const myVoucherDocument = await mongooseService.updateMyVoucherStatus(userVoucherID, status)
} catch (error) {
Expand All @@ -103,6 +103,12 @@ class UserVoucherController {
res.status(200).send({ updated: true })
}

static async getAllVouchers(req, res, next) {
const vouchersDocuments = await mongooseService.getAllVouchers();

res.status(200).send({ vouchersDocuments })
}

}

module.exports = UserVoucherController;
module.exports = UserVoucherController;
8 changes: 4 additions & 4 deletions src/api/middlewares/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Authentication {

const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]

if (token == null) {
return next(new APIError(401, 'Unauthorized.'))
}
Expand All @@ -19,7 +19,7 @@ class Authentication {
} catch (error) {
return next(new APIError(403, 'Forbidden.'))
}

next();
}

Expand All @@ -38,7 +38,7 @@ class Authentication {
// return next(new APIError(403, 'Forbidden.'))
// }

res.locals.events = req.body;
res.locals.events = req.body;

// } catch (error) {
// console.log(error);
Expand All @@ -50,4 +50,4 @@ class Authentication {

}

module.exports = Authentication;
module.exports = Authentication;
7 changes: 5 additions & 2 deletions src/api/routes/payments-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ class UserVoucherController {

static route(expressApp) {
let router = expressApp.Router();
router.get('/check-payment/:tokenIdVoucher',
ErrorHandlers.globalErrorHandler(authenticationMiddleware.authenticateGCLOUDService),
ErrorHandlers.globalErrorHandler(paymentsController.getPaymentsByVoucherID));

router.get('/:voucherID',
ErrorHandlers.globalErrorHandler(authenticationMiddleware.authenticateToken),
ErrorHandlers.globalErrorHandler(authenticationMiddleware.authenticateToken),
ErrorHandlers.globalErrorHandler(paymentsController.getPaymentActors));

router.post('/create-payment',
Expand All @@ -20,4 +23,4 @@ class UserVoucherController {
}
}

module.exports = UserVoucherController;
module.exports = UserVoucherController;
4 changes: 4 additions & 0 deletions src/api/routes/user-vouchers-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class UserVoucherController {
ErrorHandlers.globalErrorHandler(authenticationMiddleware.authenticateToken),
ErrorHandlers.globalErrorHandler(userVoucherController.getBuyersByVoucherID));

router.get('/all',
ErrorHandlers.globalErrorHandler(authenticationMiddleware.authenticateGCLOUDService),
ErrorHandlers.globalErrorHandler(userVoucherController.getAllVouchers));

router.patch('/update',
ErrorHandlers.globalErrorHandler(authenticationMiddleware.authenticateToken),
ErrorHandlers.globalErrorHandler(userValidator.ValidateVoucherHolder),
Expand Down
6 changes: 3 additions & 3 deletions src/database/Payment/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PaymentService {
_payee: metadata._payee,
_payment: metadata._payment,
txHash: metadata.txHash
})
})

await payment.save();
}
Expand All @@ -19,7 +19,7 @@ class PaymentService {
return await Payment
.where('_tokenIdVoucher').equals(_tokenIdVoucher)
}

}

module.exports = PaymentService
module.exports = PaymentService
7 changes: 6 additions & 1 deletion src/database/UserVoucher/userVoucher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const UserVoucher = require('../models/UserVoucher')
const status = require('../../utils/userVoucherStatus')

class UserVoucherService {
static async createUserVoucher(metadata, voucherID) {
return await UserVoucher.findOneAndUpdate(
Expand Down Expand Up @@ -61,6 +62,10 @@ class UserVoucherService {
{ useFindAndModify: false, new: true, upsert: true, }
)
}

static async getAllVouchers() {
return UserVoucher.find({});
}
}

module.exports = UserVoucherService;
module.exports = UserVoucherService;
5 changes: 3 additions & 2 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const MongooseService = {
findAllUsersByVoucherID: UserVoucher.findAllUsersByVoucherID,
finalizeVoucher: UserVoucher.finalizeVoucher,
createPayment: Payment.createPayment,
getPaymentsByVoucherID: Payment.getPaymentsByVoucherID
getPaymentsByVoucherID: Payment.getPaymentsByVoucherID,
getAllVouchers: UserVoucher.getAllVouchers
}

module.exports = MongooseService;
module.exports = MongooseService;

0 comments on commit c9da81d

Please sign in to comment.