Skip to content

support customer payment event #70

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
Jan 6, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ While running tests, the index names could be overwritten using environment vari
export ES_PROJECT_INDEX=projects_test
export ES_TIMELINE_INDEX=timelines_test
export ES_METADATA_INDEX=metadata_test
export ES_CUSTOMER_PAYMENT_INDEX=customer_payments_test
```

#### Running integration tests and coverage
Expand Down
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
ES_PROJECT_INDEX: process.env.ES_PROJECT_INDEX || 'projects',
ES_TIMELINE_INDEX: process.env.ES_TIMELINE_INDEX || 'timelines',
ES_METADATA_INDEX: process.env.ES_METADATA_INDEX || 'metadata',
ES_CUSTOMER_PAYMENT_INDEX: process.env.ES_CUSTOMER_PAYMENT_INDEX || 'customer_payments',
ES_TYPE: process.env.ES_TYPE || 'doc', // ES 6.x accepts only 1 Type per index and it's mandatory to define it
ES_METADATA_DEFAULT_ID: process.env.ES_METADATA_DEFAULT_ID || 1 // use for setting default id of metadata
},
Expand Down
1 change: 1 addition & 0 deletions config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
esConfig: {
ES_PROJECT_INDEX: process.env.ES_PROJECT_INDEX || 'projects_test',
ES_TIMELINE_INDEX: process.env.ES_TIMELINE_INDEX || 'timelines_test',
ES_CUSTOMER_PAYMENT_INDEX: process.env.ES_CUSTOMER_PAYMENT_INDEX || 'customer_payments_test',
ES_METADATA_INDEX: process.env.ES_METADATA_INDEX || 'metadata_test'
}
}
57 changes: 57 additions & 0 deletions migrations/elasticsearch_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const helper = require('../src/common/helper')
const ES_PROJECT_INDEX = config.get('esConfig.ES_PROJECT_INDEX')
const ES_TIMELINE_INDEX = config.get('esConfig.ES_TIMELINE_INDEX')
const ES_METADATA_INDEX = config.get('esConfig.ES_METADATA_INDEX')
const ES_CUSTOMER_PAYMENT_INDEX = config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX')
const ES_TYPE = config.get('esConfig.ES_TYPE')

// create new elasticsearch client
Expand Down Expand Up @@ -839,6 +840,56 @@ function getRequestBody (indexName) {
}
}

const customerPaymentMapping = {
properties: {
id: {
type: 'long'
},
amount: {
type: 'long'
},
currency: {
type: 'string'
},
reference: {
type: 'string'
},
referenceId: {
type: 'string'
},
paymentIntentId: {
type: 'string'
},
clientSecret: {
type: 'string'
},
status: {
type: 'string'
},
createdAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
},
createdBy: {
type: 'integer'
},
updatedAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
},
updatedBy: {
type: 'integer'
},
deletedAt: {
type: 'date',
format: 'strict_date_optional_time||epoch_millis'
},
deletedBy: {
type: 'integer'
}
}
}

const result = {
index: indexName,
include_type_name: true,
Expand All @@ -859,6 +910,9 @@ function getRequestBody (indexName) {
case ES_TIMELINE_INDEX:
result.body.mappings[ES_TYPE] = timelineMapping
break
case ES_CUSTOMER_PAYMENT_INDEX:
result.body.mappings[ES_TYPE] = customerPaymentMapping
break
default:
throw new Error(`Invalid index name '${indexName}'`)
}
Expand All @@ -878,6 +932,9 @@ esClient.indices.delete({
// Re-create metadata index
.then(() => esClient.indices.delete({ index: ES_METADATA_INDEX, ignore: [404] }))
.then(() => esClient.indices.create(getRequestBody(ES_METADATA_INDEX)))
// Re-create customerPayment index
.then(() => esClient.indices.delete({ index: ES_CUSTOMER_PAYMENT_INDEX, ignore: [404] }))
.then(() => esClient.indices.create(getRequestBody(ES_CUSTOMER_PAYMENT_INDEX)))
.then(() => {
logger.info('elasticsearch indices synced successfully')
process.exit()
Expand Down
15 changes: 15 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const RESOURCES = {
TIMELINE: 'timeline',
MILESTONE: 'milestone',
MILESTONE_TEMPLATE: 'milestone.template',
CUSTOMER_PAYMENT: 'customer-payment',
ATTACHMENT: 'attachment'
}

Expand Down Expand Up @@ -72,6 +73,19 @@ const ATTACHMENT_TYPES = {
'LINK': 'link'
}

const CUSTOMER_PAYMENT_STATUS = {
CANCELED: 'canceled',
PROCESSING: 'processing',
REQUIRES_ACTION: 'requires_action',
REQUIRES_CAPTURE: 'requires_capture',
REQUIRES_CONFIRMATION: 'requires_confirmation',
REQUIRES_PAYMENT_METHOD: 'requires_payment_method',
SUCCEEDED: 'succeeded',
REFUNDED: 'refunded',
REFUND_FAILED: 'refund_failed',
REFUND_PENDING: 'refund_pending'
}

module.exports = {
RESOURCES,
REGEX,
Expand All @@ -80,5 +94,6 @@ module.exports = {
INVITE_STATUS,
PROJECT_MEMBER_ROLE,
MILESTONE_TEMPLATE_REFERENCES,
CUSTOMER_PAYMENT_STATUS,
ATTACHMENT_TYPES
}
2 changes: 2 additions & 0 deletions src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ProcessorServiceTimeline = require('./ProcessorServiceTimeline')
const ProcessorServiceMilestone = require('./ProcessorServiceMilestone')
const ProcessorServiceMilestoneTemplate = require('./ProcessorServiceMilestoneTemplate')
const ProcessorServiceProjectMemberInvite = require('./ProcessorServiceProjectMemberInvite')
const ProcessorServiceCustomerPayment = require('./ProcessorServiceCustomerPayment')

/**
* Create schema.
Expand Down Expand Up @@ -63,6 +64,7 @@ const MappingResourceFunction = {
[RESOURCES.TIMELINE]: ProcessorServiceTimeline,
[RESOURCES.MILESTONE]: ProcessorServiceMilestone,
[RESOURCES.MILESTONE_TEMPLATE]: ProcessorServiceMilestoneTemplate,
[RESOURCES.CUSTOMER_PAYMENT]: ProcessorServiceCustomerPayment,
[RESOURCES.PROJECT_MEMBER_INVITE]: ProcessorServiceProjectMemberInvite
}

Expand Down
92 changes: 92 additions & 0 deletions src/services/ProcessorServiceCustomerPayment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Service for customer payment Elasticsearch processor.
*/

const Joi = require('joi')
const config = require('config')
const _ = require('lodash')

const logger = require('../common/logger')
const helper = require('../common/helper')
const { CUSTOMER_PAYMENT_STATUS } = require('../constants')

const client = helper.getESClient()

/**
* create schema
* @return {Object} the schema
*/
function createIdSchema () {
return Joi.object().keys({
id: Joi.number().integer().positive().required()
}).unknown(true).required()
}

/**
* create schema
* @return {Object} the schema
*/
function createSchema () {
return createIdSchema().keys({
amount: Joi.number().integer().min(1).required(),
currency: Joi.string().required(),
paymentIntentId: Joi.string().required(),
status: Joi.string().valid(_.values(CUSTOMER_PAYMENT_STATUS)).required(),
reference: Joi.string().optional(),
referenceId: Joi.string().optional(),
createdAt: Joi.any(),
updatedAt: Joi.any(),
deletedAt: Joi.any(),
createdBy: Joi.any(),
updatedBy: Joi.any(),
deletedBy: Joi.any()
}).unknown(true).required()
}

/**
* Create message in Elasticsearch.
* @param {Object} message the customer payment created message
* @return {Promise} promise result
*/
async function create (message) {
await client.create({
index: config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: message.id,
body: message
})
logger.debug(`CustomerPayment created successfully in elasticsearch index, (customerPayment: ${JSON.stringify(message)})`)
}

create.schema = {
message: createSchema()
}

/**
* Update message in Elasticsearch.
* @param {Object} message the customer payment updated message
* @return {Promise} promise result
*/
async function update (message) {
await client.update({
index: config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX'),
type: config.get('esConfig.ES_TYPE'),
id: message.id,
body: {
doc: message
}
})
logger.debug(`CustomerPayment updated successfully in elasticsearch index, (customerPayment: ${message.id})`)
}

update.schema = {
message: createSchema()
}

// Exports
module.exports = {
create,
update
}

logger.buildService(module.exports)