Skip to content

Commit

Permalink
Merge pull request #605 from setlife-network/release/1.3
Browse files Browse the repository at this point in the history
Release/1.3
  • Loading branch information
otech47 committed Jan 18, 2022
2 parents b4a0515 + d930219 commit 88fe358
Show file tree
Hide file tree
Showing 73 changed files with 18,190 additions and 17,157 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
name: Install Node & Run Unit Tests

on:
workflow_dispatch:
push:
branches: [ develop ]
pull_request:
Expand All @@ -16,7 +17,7 @@ jobs:

strategy:
matrix:
node-version: [12.x]
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand All @@ -26,5 +27,5 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm install
- run: npm test
3 changes: 1 addition & 2 deletions api/config/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ module.exports = {
SECRET: process.env.STRIPE_SECRET,
},
TOGGL: {
API_KEY: process.env.TOGGL_API_KEY,
WORKSPACE_ID: process.env.TOGGL_WORKSPACE_ID
API_KEY: process.env.TOGGL_API_KEY
},
// MYSQL Access Keys
MYSQL: {
Expand Down
25 changes: 18 additions & 7 deletions api/handlers/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const stripeHandler = module.exports = (() => {
const createCustomer = async (params) => {
const { email, name } = params

stripeClient.customers.create({
return stripeClient.customers.create({
email,
name,
})
Expand Down Expand Up @@ -43,10 +43,9 @@ const stripeHandler = module.exports = (() => {
actualCurrency,
external_uuid
} = params

const client = await clientManagement.findClientWithId(clientId)
const clientExternalUuid = external_uuid ? external_uuid : await clientManagement.findClientWithId(clientId)
const invoiceItemProps = {
customer: client.external_uuid,
customer: clientExternalUuid,
currency: actualCurrency,
price_data: {
currency: actualCurrency,
Expand All @@ -56,11 +55,16 @@ const stripeHandler = module.exports = (() => {
}
const invoiceProps = {
collection_method: 'charge_automatically',
customer: client.external_uuid,
customer: clientExternalUuid,
description: 'payment charged from trinary'
}
const invoiceItem = await stripeClient.invoiceItems.create(invoiceItemProps)
return stripeClient.invoices.create(invoiceProps)
try {
const invoiceItem = await stripeClient.invoiceItems.create(invoiceItemProps)
return stripeClient.invoices.create(invoiceProps)
} catch (err) {
console.log('An error ocurred: ', err)
}

}

const finalizeInvoice = async (params) => {
Expand All @@ -70,6 +74,12 @@ const stripeHandler = module.exports = (() => {
return stripeClient.invoices.finalizeInvoice(invoice.id)
}

const listAllCustomers = async () => {
return stripeClient.customers.list({
limit: 100,
});
}

const updateCustomerWithClientId = async (params) => {
const { clientId } = params

Expand All @@ -95,6 +105,7 @@ const stripeHandler = module.exports = (() => {
checkCredentials,
createInvoice,
finalizeInvoice,
listAllCustomers,
updateCustomerWithClientId
}

Expand Down
44 changes: 44 additions & 0 deletions api/migrations/20211012181841-createContributions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.createTable('Contributions', {
id: {
type: Sequelize.DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
contributor_id: {
type: Sequelize.DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Contributors',
key: 'id'
}
},
issue_id: {
type: Sequelize.DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Issues',
key: 'id'
}
},
is_author: {
type: Sequelize.DataTypes.INTEGER,
allowNull: false
},
is_assigned: {
type: Sequelize.DataTypes.INTEGER,
allowNull: false
},
date_contributed: {
type: Sequelize.DataTypes.DATE,
allowNull: false
},
});
},

down: async (queryInterface, Sequelize) => {
return queryInterface.dropTable('Contributions');
}
};
21 changes: 21 additions & 0 deletions api/migrations/20220107192315-addConstraintToClientEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.addConstraint('Clients', {
fields: ['email'],
type: 'unique',
name: 'unique_client_email'
}, { transaction: t })
])
})
},

down: async (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.removeConstraint('Clients', 'unique_client_email', { transaction: t })
])
})
}
};
3 changes: 2 additions & 1 deletion api/models/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = (sequelize) => {
allowNull: false
},
email: {
type: DataTypes.STRING
type: DataTypes.STRING,
unique: true
},
currency: {
type: DataTypes.STRING,
Expand Down
55 changes: 55 additions & 0 deletions api/models/Contribution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Sequelize = require('sequelize')
const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {

class Contribution extends Sequelize.Model {}

Contribution.init({
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
contributor_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Contributors',
key: 'id'
}
},
issue_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Issues',
key: 'id'
}
},
is_author: {
type: DataTypes.INTEGER,
allowNull: false
},
is_assigned: {
type: DataTypes.INTEGER,
allowNull: false
},
date_contributed: {
type: DataTypes.DATE,
allowNull: false
}
},
{
// Model options go here
sequelize,
modelName: 'Contribution',
createdAt: 'created_at',
updatedAt: 'updated_at'
})

return Contribution

}
26 changes: 20 additions & 6 deletions api/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const db = {
models: {
Allocation: require('./Allocation')(sequelize),
Client: require('./Client')(sequelize),
Contribution: require('./Contribution')(sequelize),
Contributor: require('./Contributor')(sequelize),
Issue: require('./Issue')(sequelize),
Payment: require('./Payment')(sequelize),
Expand All @@ -51,20 +52,33 @@ const db = {
};

//Associations
const associations = ({ Allocation, Client, Contributor, Issue, Payment, Permission, Project, Rate, TimeEntry }) => {
const associations = ({
Allocation,
Client,
Contribution,
Contributor,
Issue,
Payment,
Permission,
Project,
Rate,
TimeEntry
}) => {
Client.hasMany(Payment, { foreignKey: 'client_id' });
Client.hasMany(Permission, { foreignKey: 'client_id' })
Client.hasMany(Project, { foreignKey: 'client_id' })
Contribution.belongsTo(Contributor, { foreignKey: 'contributor_id' })
Contribution.belongsTo(Issue, { foreignKey: 'issue_id' })
Contributor.hasMany(Allocation, { foreignKey: 'contributor_id' })
Contributor.hasMany(Permission, { foreignKey: 'contributor_id' })
Contributor.hasMany(TimeEntry, { foreignKey: 'contributor_id' })
Issue.belongsTo(Project, { foreignKey: 'project_id' })
Payment.hasMany(Allocation, { foreignKey: 'payment_id' })
Project.hasMany(Allocation, { foreignKey: 'project_id' })
Client.hasMany(Project, { foreignKey: 'client_id' })
Client.hasMany(Permission, { foreignKey: 'client_id' })
Project.hasMany(Permission, { foreignKey: 'project_id' })
Issue.belongsTo(Project, { foreignKey: 'project_id' })
Project.hasMany(TimeEntry, { foreignKey: 'project_id' })
Rate.hasMany(Allocation, { foreignKey: 'rate_id' })
Rate.belongsTo(Contributor, { foreignKey: 'contributor_id' })
Contributor.hasMany(TimeEntry, { foreignKey: 'contributor_id' })
Project.hasMany(TimeEntry, { foreignKey: 'project_id' })
}

associations(db.models)
Expand Down
19 changes: 15 additions & 4 deletions api/modules/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,32 @@ const authentication = module.exports = (() => {
}))
}

const updateGithubAccessTokenContributor = async (githubContributor) => {
const updateContributorGithubAccessTokenByGithubId = async ({ githubId, githubAccessToken }) => {
const contributor = await db.models.Contributor.findOne({
where: {
github_id: githubContributor.id
github_id: githubId
}
})
contributor.github_access_token = githubAccessToken
return contributor.save()
}

const updateContributorGithubHandle = async ({ contributorId, githubHandle }) => {
const contributor = await db.models.Contributor.findOne({
where: {
id: contributorId
}
})
contributor.github_access_token = githubContributor.accessToken
contributor.github_handle = githubHandle
return contributor.save()
}

return {
createContributor,
getContributor,
grantProjectPermissions,
updateGithubAccessTokenContributor
updateContributorGithubHandle,
updateContributorGithubAccessTokenByGithubId,
}

})()
51 changes: 26 additions & 25 deletions api/modules/budgeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ const budgeting = module.exports = (() => {
const clientManagement = require('./clientManagement')

const checkForMismatchedClientPayments = async (params) => {
const existingPayments = await getPaymentsWithClientId({ client_id: params.client.id })

if (existingPayments) {
existingPayments.forEach(async payment => {

if (params.client.currency !== params.stripeInvoiceCurrency) {
try {
const disconnectedCustomer = await clientManagement.deleteClientUuid({ id: params.client.external_uuid })
console.log(`stripe customer disconnected ${disconnectedCustomer.email}`)
} catch (err) {
console.log(`An error ocurred: ${err}`)
}
}
})
const {
client,
stripeInvoiceCurrency
} = params
const existingPayments = await getPaymentsWithClientId({ client_id: client.id })
if (
existingPayments.length > 0 &&
client.currency.toUpperCase() != stripeInvoiceCurrency.toUpperCase()
) {
try {
const disconnectedCustomer = await clientManagement.deleteClientUuid({ id: client.external_uuid })
console.log(`stripe customer disconnected ${disconnectedCustomer.email}`)
} catch (err) {
console.log(`An error ocurred: ${err}`)
}
}
}

Expand Down Expand Up @@ -100,16 +101,16 @@ const budgeting = module.exports = (() => {

// Disable currency check in production until bug is fixed

// try {
// const paymentsDoNotMatch = await checkForMismatchedClientPayments({
// client,
// stripeInvoiceCurrency: stripeInvoice.currency
// })
// client.currency = stripeInvoice.currency.toUpperCase()
// await client.save()
// } catch (err) {
// console.log(`error while changing client currency: ${err}`)
// }
try {
const paymentsDoNotMatch = await checkForMismatchedClientPayments({
client,
stripeInvoiceCurrency: stripeInvoice.currency
})
client.currency = stripeInvoice.currency.toUpperCase()
await client.save()
} catch (err) {
console.log(`error while changing client currency: ${err}`)
}

if (paymentToUpdate) {
const datePaidOverride = stripeInvoice.metadata.date_paid || null
Expand Down Expand Up @@ -163,7 +164,7 @@ const budgeting = module.exports = (() => {
})
}
}

return {
createPaymentFromStripeInvoice,
deletePaymentByStripeInvoiceId,
Expand Down
Loading

0 comments on commit 88fe358

Please sign in to comment.