Skip to content

Commit aff1a3b

Browse files
committed
support customer payment event
1 parent bc853a8 commit aff1a3b

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ While running tests, the index names could be overwritten using environment vari
184184
export ES_PROJECT_INDEX=projects_test
185185
export ES_TIMELINE_INDEX=timelines_test
186186
export ES_METADATA_INDEX=metadata_test
187+
export ES_CUSTOMER_PAYMENT_INDEX=customer_payments_test
187188
```
188189

189190
#### Running integration tests and coverage

config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
ES_PROJECT_INDEX: process.env.ES_PROJECT_INDEX || 'projects',
3434
ES_TIMELINE_INDEX: process.env.ES_TIMELINE_INDEX || 'timelines',
3535
ES_METADATA_INDEX: process.env.ES_METADATA_INDEX || 'metadata',
36+
ES_CUSTOMER_PAYMENT_INDEX: process.env.ES_CUSTOMER_PAYMENT_INDEX || 'customer_payments',
3637
ES_TYPE: process.env.ES_TYPE || 'doc', // ES 6.x accepts only 1 Type per index and it's mandatory to define it
3738
ES_METADATA_DEFAULT_ID: process.env.ES_METADATA_DEFAULT_ID || 1 // use for setting default id of metadata
3839
},

config/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
esConfig: {
77
ES_PROJECT_INDEX: process.env.ES_PROJECT_INDEX || 'projects_test',
88
ES_TIMELINE_INDEX: process.env.ES_TIMELINE_INDEX || 'timelines_test',
9+
ES_CUSTOMER_PAYMENT_INDEX: process.env.ES_CUSTOMER_PAYMENT_INDEX || 'customer_payments_test',
910
ES_METADATA_INDEX: process.env.ES_METADATA_INDEX || 'metadata_test'
1011
}
1112
}

migrations/elasticsearch_sync.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const helper = require('../src/common/helper')
1616
const ES_PROJECT_INDEX = config.get('esConfig.ES_PROJECT_INDEX')
1717
const ES_TIMELINE_INDEX = config.get('esConfig.ES_TIMELINE_INDEX')
1818
const ES_METADATA_INDEX = config.get('esConfig.ES_METADATA_INDEX')
19+
const ES_CUSTOMER_PAYMENT_INDEX = config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX')
1920
const ES_TYPE = config.get('esConfig.ES_TYPE')
2021

2122
// create new elasticsearch client
@@ -839,6 +840,56 @@ function getRequestBody (indexName) {
839840
}
840841
}
841842

843+
const customerPaymentMapping = {
844+
properties: {
845+
id: {
846+
type: 'long'
847+
},
848+
amount: {
849+
type: 'long'
850+
},
851+
currency: {
852+
type: 'string'
853+
},
854+
reference: {
855+
type: 'string'
856+
},
857+
referenceId: {
858+
type: 'string'
859+
},
860+
paymentIntentId: {
861+
type: 'string'
862+
},
863+
clientSecret: {
864+
type: 'string'
865+
},
866+
status: {
867+
type: 'string'
868+
},
869+
createdAt: {
870+
type: 'date',
871+
format: 'strict_date_optional_time||epoch_millis'
872+
},
873+
createdBy: {
874+
type: 'integer'
875+
},
876+
updatedAt: {
877+
type: 'date',
878+
format: 'strict_date_optional_time||epoch_millis'
879+
},
880+
updatedBy: {
881+
type: 'integer'
882+
},
883+
deletedAt: {
884+
type: 'date',
885+
format: 'strict_date_optional_time||epoch_millis'
886+
},
887+
deletedBy: {
888+
type: 'integer'
889+
}
890+
}
891+
}
892+
842893
const result = {
843894
index: indexName,
844895
include_type_name: true,
@@ -859,6 +910,9 @@ function getRequestBody (indexName) {
859910
case ES_TIMELINE_INDEX:
860911
result.body.mappings[ES_TYPE] = timelineMapping
861912
break
913+
case ES_CUSTOMER_PAYMENT_INDEX:
914+
result.body.mappings[ES_TYPE] = customerPaymentMapping
915+
break
862916
default:
863917
throw new Error(`Invalid index name '${indexName}'`)
864918
}
@@ -878,6 +932,9 @@ esClient.indices.delete({
878932
// Re-create metadata index
879933
.then(() => esClient.indices.delete({ index: ES_METADATA_INDEX, ignore: [404] }))
880934
.then(() => esClient.indices.create(getRequestBody(ES_METADATA_INDEX)))
935+
// Re-create customerPayment index
936+
.then(() => esClient.indices.delete({ index: ES_CUSTOMER_PAYMENT_INDEX, ignore: [404] }))
937+
.then(() => esClient.indices.create(getRequestBody(ES_CUSTOMER_PAYMENT_INDEX)))
881938
.then(() => {
882939
logger.info('elasticsearch indices synced successfully')
883940
process.exit()

src/constants.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const RESOURCES = {
3232
TIMELINE: 'timeline',
3333
MILESTONE: 'milestone',
3434
MILESTONE_TEMPLATE: 'milestone.template',
35+
CUSTOMER_PAYMENT: 'customer-payment',
3536
ATTACHMENT: 'attachment'
3637
}
3738

@@ -72,6 +73,19 @@ const ATTACHMENT_TYPES = {
7273
'LINK': 'link'
7374
}
7475

76+
const CUSTOMER_PAYMENT_STATUS = {
77+
CANCELED: 'canceled',
78+
PROCESSING: 'processing',
79+
REQUIRES_ACTION: 'requires_action',
80+
REQUIRES_CAPTURE: 'requires_capture',
81+
REQUIRES_CONFIRMATION: 'requires_confirmation',
82+
REQUIRES_PAYMENT_METHOD: 'requires_payment_method',
83+
SUCCEEDED: 'succeeded',
84+
REFUNDED: 'refunded',
85+
REFUND_FAILED: 'refund_failed',
86+
REFUND_PENDING: 'refund_pending'
87+
}
88+
7589
module.exports = {
7690
RESOURCES,
7791
REGEX,
@@ -80,5 +94,6 @@ module.exports = {
8094
INVITE_STATUS,
8195
PROJECT_MEMBER_ROLE,
8296
MILESTONE_TEMPLATE_REFERENCES,
97+
CUSTOMER_PAYMENT_STATUS,
8398
ATTACHMENT_TYPES
8499
}

src/services/ProcessorService.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const ProcessorServiceTimeline = require('./ProcessorServiceTimeline')
2626
const ProcessorServiceMilestone = require('./ProcessorServiceMilestone')
2727
const ProcessorServiceMilestoneTemplate = require('./ProcessorServiceMilestoneTemplate')
2828
const ProcessorServiceProjectMemberInvite = require('./ProcessorServiceProjectMemberInvite')
29+
const ProcessorServiceCustomerPayment = require('./ProcessorServiceCustomerPayment')
2930

3031
/**
3132
* Create schema.
@@ -63,6 +64,7 @@ const MappingResourceFunction = {
6364
[RESOURCES.TIMELINE]: ProcessorServiceTimeline,
6465
[RESOURCES.MILESTONE]: ProcessorServiceMilestone,
6566
[RESOURCES.MILESTONE_TEMPLATE]: ProcessorServiceMilestoneTemplate,
67+
[RESOURCES.CUSTOMER_PAYMENT]: ProcessorServiceCustomerPayment,
6668
[RESOURCES.PROJECT_MEMBER_INVITE]: ProcessorServiceProjectMemberInvite
6769
}
6870

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Service for customer payment Elasticsearch processor.
3+
*/
4+
5+
const Joi = require('joi')
6+
const config = require('config')
7+
const _ = require('lodash')
8+
9+
const logger = require('../common/logger')
10+
const helper = require('../common/helper')
11+
const { CUSTOMER_PAYMENT_STATUS } = require('../constants')
12+
13+
const client = helper.getESClient()
14+
15+
/**
16+
* create schema
17+
* @return {Object} the schema
18+
*/
19+
function createIdSchema () {
20+
return Joi.object().keys({
21+
id: Joi.number().integer().positive().required()
22+
}).unknown(true).required()
23+
}
24+
25+
/**
26+
* create schema
27+
* @return {Object} the schema
28+
*/
29+
function createSchema () {
30+
return createIdSchema().keys({
31+
amount: Joi.number().integer().min(1).required(),
32+
currency: Joi.string().required(),
33+
paymentIntentId: Joi.string().required(),
34+
status: Joi.string().valid(_.values(CUSTOMER_PAYMENT_STATUS)).required(),
35+
reference: Joi.string().optional(),
36+
referenceId: Joi.string().optional(),
37+
createdAt: Joi.any(),
38+
updatedAt: Joi.any(),
39+
deletedAt: Joi.any(),
40+
createdBy: Joi.any(),
41+
updatedBy: Joi.any(),
42+
deletedBy: Joi.any()
43+
}).unknown(true).required()
44+
}
45+
46+
/**
47+
* Create message in Elasticsearch.
48+
* @param {Object} message the customer payment created message
49+
* @return {Promise} promise result
50+
*/
51+
async function create (message) {
52+
await client.create({
53+
index: config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX'),
54+
type: config.get('esConfig.ES_TYPE'),
55+
id: message.id,
56+
body: message
57+
})
58+
logger.debug(`CustomerPayment created successfully in elasticsearch index, (customerPayment: ${JSON.stringify(message)})`)
59+
}
60+
61+
create.schema = {
62+
message: createSchema()
63+
}
64+
65+
/**
66+
* Update message in Elasticsearch.
67+
* @param {Object} message the customer payment updated message
68+
* @return {Promise} promise result
69+
*/
70+
async function update (message) {
71+
await client.update({
72+
index: config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX'),
73+
type: config.get('esConfig.ES_TYPE'),
74+
id: message.id,
75+
body: {
76+
doc: message
77+
}
78+
})
79+
logger.debug(`CustomerPayment updated successfully in elasticsearch index, (customerPayment: ${message.id})`)
80+
}
81+
82+
update.schema = {
83+
message: createSchema()
84+
}
85+
86+
// Exports
87+
module.exports = {
88+
create,
89+
update
90+
}
91+
92+
logger.buildService(module.exports)

0 commit comments

Comments
 (0)