-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payments): body payments + testing
- Loading branch information
1 parent
d1796fc
commit 5d14e76
Showing
17 changed files
with
1,054 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,76 @@ | ||
const { Payment, User, BodyMembership } = require('../models'); | ||
const errors = require('../lib/errors'); | ||
const helpers = require('../lib/helpers'); | ||
const constants = require('../lib/constants'); | ||
|
||
exports.listAllPayments = async (req, res) => { | ||
if (!req.permissions.hasPermission('view:payment')) { | ||
return errors.makeForbiddenError(res, 'Permission view:payment is required, but not present.'); | ||
} | ||
|
||
const result = await Payment.findAndCountAll({ | ||
where: { body_id: req.currentBody.id }, | ||
...helpers.getPagination(req.query), | ||
order: helpers.getSorting(req.query) | ||
}); | ||
|
||
return res.json({ | ||
success: true, | ||
data: result.rows, | ||
meta: { count: result.count } | ||
}); | ||
}; | ||
|
||
exports.createPayment = async (req, res) => { | ||
if (!req.permissions.hasPermission('create:payment')) { | ||
return errors.makeForbiddenError(res, 'Permission create:payment is required, but not present.'); | ||
} | ||
|
||
const user = await User.findOne({ | ||
where: { id: req.body.user_id }, | ||
include: [BodyMembership] | ||
}); | ||
|
||
if (!user) { | ||
return errors.makeNotFoundError(res, 'User is not found.'); | ||
} | ||
|
||
if (!user.body_memberships.some((membership) => membership.body_id === req.currentBody.id)) { | ||
return errors.makeForbiddenError(res, 'User is not a member of a body.'); | ||
} | ||
|
||
const payment = await Payment.create({ | ||
...req.body, | ||
user_id: user.id, | ||
body_id: req.currentBody.id | ||
}); | ||
|
||
return res.json({ | ||
success: true, | ||
data: payment | ||
}); | ||
}; | ||
|
||
exports.updatePayment = async (req, res) => { | ||
if (!req.permissions.hasPermission('update:payment')) { | ||
return errors.makeForbiddenError(res, 'Permission update:payment is required, but not present.'); | ||
} | ||
|
||
await req.currentPayment.update(req.body, { fields: constants.FIELDS_TO_UPDATE.PAYMENT.UPDATE }); | ||
return res.json({ | ||
success: true, | ||
data: req.currentPayment | ||
}); | ||
}; | ||
|
||
exports.deletePayment = async (req, res) => { | ||
if (!req.permissions.hasPermission('delete:payment')) { | ||
return errors.makeForbiddenError(res, 'Permission delete:payment is required, but not present.'); | ||
} | ||
|
||
await req.currentPayment.destroy(); | ||
return res.json({ | ||
success: true, | ||
message: 'Payment is deleted.' | ||
}); | ||
}; |
This file contains 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,65 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('payments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE' | ||
}, | ||
body_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'bodies', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE' | ||
}, | ||
starts: { | ||
type: Sequelize.DATEONLY, | ||
allowNull: false, | ||
}, | ||
expires: { | ||
type: Sequelize.DATEONLY, | ||
allowNull: false, | ||
}, | ||
amount: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}, | ||
currency: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
comment: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
}, | ||
invoice_name: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
}, | ||
invoice_address: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface) => queryInterface.dropTable('payments') | ||
}; |
This file contains 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 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,73 @@ | ||
const moment = require('moment'); | ||
|
||
const { Sequelize, sequelize } = require('../lib/sequelize'); | ||
|
||
const Payment = sequelize.define('payment', { | ||
starts: { | ||
type: Sequelize.DATEONLY, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Starts date should be set.' }, | ||
isDate: { msg: 'Starts date should be valid.' } | ||
} | ||
}, | ||
expires: { | ||
type: Sequelize.DATEONLY, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Expiration date should be set.' }, | ||
isDate: { msg: 'Expiration date should be valid.' }, | ||
laterThanStarts(val) { | ||
if (moment(val).isSameOrBefore(this.starts)) { | ||
throw new Error('Expiration date should be after the start date.'); | ||
} | ||
}, | ||
} | ||
}, | ||
amount: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Amount should be set.' }, | ||
isInt: { msg: 'Amount should be valid.' }, | ||
min: { args: [0], msg: 'Amount cannot be negative.' } | ||
} | ||
}, | ||
currency: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
validate: { | ||
notEmpty: { msg: 'Currency should be set.' } | ||
} | ||
}, | ||
comment: { | ||
type: Sequelize.TEXT, | ||
allowNull: true | ||
}, | ||
invoice_name: { | ||
type: Sequelize.TEXT, | ||
allowNull: true | ||
}, | ||
invoice_address: { | ||
type: Sequelize.TEXT, | ||
allowNull: true | ||
} | ||
}, { | ||
underscored: true, | ||
tableName: 'payments', | ||
createdAt: 'created_at', | ||
updatedAt: 'updated_at', | ||
}); | ||
|
||
Payment.beforeValidate(async (payment) => { | ||
// skipping these fields if they are unset, will catch it later. | ||
if (typeof payment.currency === 'string') payment.currency = payment.currency.trim(); | ||
if (typeof payment.comment === 'string') payment.comment = payment.comment.trim(); | ||
if (typeof payment.invoice_name === 'string') payment.invoice_name = payment.invoice_name.trim(); | ||
if (typeof payment.invoice_address === 'string') payment.invoice_address = payment.invoice_address.trim(); | ||
}); | ||
|
||
module.exports = Payment; |
This file contains 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
Oops, something went wrong.