Skip to content

Commit 830de0e

Browse files
authored
Merge pull request #30 from eisbilir/challenge-work-period
work period endpoint
2 parents a974a4e + 6d1b0df commit 830de0e

12 files changed

+217
-9
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ The following parameters can be set in config files or in env variables:
3030
- `topics.TAAS_RESOURCE_BOOKING_CREATE_TOPIC`: the create resource booking entity Kafka message topic
3131
- `topics.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC`: the update resource booking entity Kafka message topic
3232
- `topics.TAAS_RESOURCE_BOOKING_DELETE_TOPIC`: the delete resource booking entity Kafka message topic
33+
- `topics.TAAS_WORK_PERIOD_CREATE_TOPIC`: the create work period entity Kafka message topic
34+
- `topics.TAAS_WORK_PERIOD_UPDATE_TOPIC`: the update work period entity Kafka message topic
35+
- `topics.TAAS_WORK_PERIOD_DELETE_TOPIC`: the delete work period entity Kafka message topic
3336
- `esConfig.HOST`: Elasticsearch host
3437
- `esConfig.AWS_REGION`: The Amazon region to use when using AWS Elasticsearch service
3538
- `esConfig.ELASTICCLOUD.id`: The elastic cloud id, if your elasticsearch instance is hosted on elastic cloud. DO NOT provide a value for ES_HOST if you are using this
@@ -38,6 +41,7 @@ The following parameters can be set in config files or in env variables:
3841
- `esConfig.ES_INDEX_JOB`: the index name for job
3942
- `esConfig.ES_INDEX_JOB_CANDIDATE`: the index name for job candidate
4043
- `esConfig.ES_INDEX_RESOURCE_BOOKING`: the index name for resource booking
44+
- `esConfig.ES_INDEX_WORK_PERIOD`: the index name for work period
4145

4246
- `auth0.AUTH0_URL`: Auth0 URL, used to get TC M2M token
4347
- `auth0.AUTH0_AUDIENCE`: Auth0 audience, used to get TC M2M token

VERIFICATION.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Create documents in ES
44

5-
- Run the following commands to create `Job`, `JobCandidate` and `ResourceBooking` documents in ES.
5+
- Run the following commands to create `Job`, `JobCandidate`, `ResourceBooking`, `WorkPeriod` documents in ES.
66

77
``` bash
88
# for Job
@@ -11,12 +11,14 @@
1111
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.jobcandidate.create < test/messages/taas.jobcandidate.create.event.json
1212
# for ResourceBooking
1313
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.resourcebooking.create < test/messages/taas.resourcebooking.create.event.json
14+
# for WorkPeriod
15+
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.workperiod.create < test/messages/taas.workperiod.create.event.json
1416
```
1517

1618
- Run `npm run view-data <model-name-here>` to see if documents were created.
1719

1820
## Update documents in ES
19-
- Run the following commands to update `Job`, `JobCandidate` and `ResourceBooking` documents in ES.
21+
- Run the following commands to update `Job`, `JobCandidate`, `ResourceBooking`, `WorkPeriod` documents in ES.
2022

2123
``` bash
2224
# for Job
@@ -25,12 +27,14 @@
2527
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.jobcandidate.update < test/messages/taas.jobcandidate.update.event.json
2628
# for ResourceBooking
2729
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.resourcebooking.update < test/messages/taas.resourcebooking.update.event.json
30+
# for WorkPeriod
31+
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.workperiod.update < test/messages/taas.workperiod.update.event.json
2832
```
2933

3034
- Run `npm run view-data <model-name-here>` to see if documents were updated.
3135

3236
## Delete documents in ES
33-
- Run the following commands to delete `Job`, `JobCandidate` and `ResourceBooking` documents in ES.
37+
- Run the following commands to delete `Job`, `JobCandidate`, `ResourceBooking`, `WorkPeriod` documents in ES.
3438

3539
``` bash
3640
# for Job
@@ -39,6 +43,8 @@
3943
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.jobcandidate.delete < test/messages/taas.jobcandidate.delete.event.json
4044
# for ResourceBooking
4145
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.resourcebooking.delete < test/messages/taas.resourcebooking.delete.event.json
46+
# for WorkPeriod
47+
docker exec -i taas-es-processor_kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic taas.workperiod.delete < test/messages/taas.workperiod.delete.event.json
4248
```
4349

4450
- Run `npm run view-data <model-name-here>` to see if documents were deleted.

config/default.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ module.exports = {
1616
KAFKA_GROUP_ID: process.env.KAFKA_GROUP_ID || 'taas-es-processor',
1717

1818
topics: {
19+
// topics for job service
1920
TAAS_JOB_CREATE_TOPIC: process.env.TAAS_JOB_CREATE_TOPIC || 'taas.job.create',
2021
TAAS_JOB_UPDATE_TOPIC: process.env.TAAS_JOB_UPDATE_TOPIC || 'taas.job.update',
2122
TAAS_JOB_DELETE_TOPIC: process.env.TAAS_JOB_DELETE_TOPIC || 'taas.job.delete',
2223
// topics for jobcandidate service
2324
TAAS_JOB_CANDIDATE_CREATE_TOPIC: process.env.TAAS_JOB_CANDIDATE_CREATE_TOPIC || 'taas.jobcandidate.create',
2425
TAAS_JOB_CANDIDATE_UPDATE_TOPIC: process.env.TAAS_JOB_CANDIDATE_UPDATE_TOPIC || 'taas.jobcandidate.update',
2526
TAAS_JOB_CANDIDATE_DELETE_TOPIC: process.env.TAAS_JOB_CANDIDATE_DELETE_TOPIC || 'taas.jobcandidate.delete',
26-
// topics for job service
27+
// topics for resource booking service
2728
TAAS_RESOURCE_BOOKING_CREATE_TOPIC: process.env.TAAS_RESOURCE_BOOKING_CREATE_TOPIC || 'taas.resourcebooking.create',
2829
TAAS_RESOURCE_BOOKING_UPDATE_TOPIC: process.env.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC || 'taas.resourcebooking.update',
29-
TAAS_RESOURCE_BOOKING_DELETE_TOPIC: process.env.TAAS_RESOURCE_BOOKING_DELETE_TOPIC || 'taas.resourcebooking.delete'
30+
TAAS_RESOURCE_BOOKING_DELETE_TOPIC: process.env.TAAS_RESOURCE_BOOKING_DELETE_TOPIC || 'taas.resourcebooking.delete',
31+
// topics for work period service
32+
TAAS_WORK_PERIOD_CREATE_TOPIC: process.env.TAAS_WORK_PERIOD_CREATE_TOPIC || 'taas.workperiod.create',
33+
TAAS_WORK_PERIOD_UPDATE_TOPIC: process.env.TAAS_WORK_PERIOD_UPDATE_TOPIC || 'taas.workperiod.update',
34+
TAAS_WORK_PERIOD_DELETE_TOPIC: process.env.TAAS_WORK_PERIOD_DELETE_TOPIC || 'taas.workperiod.delete'
3035
},
3136

3237
esConfig: {
@@ -42,7 +47,8 @@ module.exports = {
4247

4348
ES_INDEX_JOB: process.env.ES_INDEX_JOB || 'job',
4449
ES_INDEX_JOB_CANDIDATE: process.env.ES_INDEX_JOB_CANDIDATE || 'job_candidate',
45-
ES_INDEX_RESOURCE_BOOKING: process.env.ES_INDEX_RESOURCE_BOOKING || 'resource_booking'
50+
ES_INDEX_RESOURCE_BOOKING: process.env.ES_INDEX_RESOURCE_BOOKING || 'resource_booking',
51+
ES_INDEX_WORK_PERIOD: process.env.ES_INDEX_WORK_PERIOD || 'work_period'
4652
},
4753

4854
auth0: {

src/app.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const helper = require('./common/helper')
1212
const JobProcessorService = require('./services/JobProcessorService')
1313
const JobCandidateProcessorService = require('./services/JobCandidateProcessorService')
1414
const ResourceBookingProcessorService = require('./services/ResourceBookingProcessorService')
15+
const WorkPeriodProcessorService = require('./services/WorkPeriodProcessorService')
1516
const Mutex = require('async-mutex').Mutex
1617
const events = require('events')
1718

@@ -38,7 +39,11 @@ const topicServiceMapping = {
3839
// resource booking
3940
[config.topics.TAAS_RESOURCE_BOOKING_CREATE_TOPIC]: ResourceBookingProcessorService.processCreate,
4041
[config.topics.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC]: ResourceBookingProcessorService.processUpdate,
41-
[config.topics.TAAS_RESOURCE_BOOKING_DELETE_TOPIC]: ResourceBookingProcessorService.processDelete
42+
[config.topics.TAAS_RESOURCE_BOOKING_DELETE_TOPIC]: ResourceBookingProcessorService.processDelete,
43+
// work period
44+
[config.topics.TAAS_WORK_PERIOD_CREATE_TOPIC]: WorkPeriodProcessorService.processCreate,
45+
[config.topics.TAAS_WORK_PERIOD_UPDATE_TOPIC]: WorkPeriodProcessorService.processUpdate,
46+
[config.topics.TAAS_WORK_PERIOD_DELETE_TOPIC]: WorkPeriodProcessorService.processDelete
4247
}
4348

4449
// Start kafka consumer

src/bootstrap.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Joi.jobStatus = () => Joi.string().valid('sourcing', 'in-review', 'assigned', 'c
99
Joi.jobCandidateStatus = () => Joi.string().valid('open', 'selected', 'shortlist', 'rejected', 'cancelled', 'interview')
1010
Joi.workload = () => Joi.string().valid('full-time', 'fractional')
1111
Joi.title = () => Joi.string().max(128)
12+
Joi.paymentStatus = () => Joi.string().valid('pending', 'partially-completed', 'completed', 'cancelled')
1213
// Empty string is not allowed by Joi by default and must be enabled with allow('').
1314
// See https://joi.dev/api/?v=17.3.0#string fro details why it's like this.
1415
// In many cases we would like to allow empty string to make it easier to create UI for editing data.

src/scripts/createIndex.js

+23
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ async function createIndex () {
7575
}
7676
}
7777
}
78+
},
79+
{
80+
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
81+
body: {
82+
mappings: {
83+
properties: {
84+
resourceBookingId: { type: 'keyword' },
85+
userHandle: { type: 'keyword' },
86+
projectId: { type: 'integer' },
87+
userId: { type: 'keyword' },
88+
startDate: { type: 'date', format: 'yyyy-MM-dd' },
89+
endDate: { type: 'date', format: 'yyyy-MM-dd' },
90+
daysWorked: { type: 'integer' },
91+
memberRate: { type: 'float' },
92+
customerRate: { type: 'float' },
93+
paymentStatus: { type: 'keyword' },
94+
createdAt: { type: 'date' },
95+
createdBy: { type: 'keyword' },
96+
updatedAt: { type: 'date' },
97+
updatedBy: { type: 'keyword' }
98+
}
99+
}
100+
}
78101
}]
79102

80103
for (const index of indices) {

src/scripts/deleteIndex.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ async function deleteIndex () {
1111
const esClient = helper.getESClient()
1212
const indices = [config.get('esConfig.ES_INDEX_JOB'),
1313
config.get('esConfig.ES_INDEX_JOB_CANDIDATE'),
14-
config.get('esConfig.ES_INDEX_RESOURCE_BOOKING')]
14+
config.get('esConfig.ES_INDEX_RESOURCE_BOOKING'),
15+
config.get('esConfig.ES_INDEX_WORK_PERIOD')]
1516
for (const index of indices) {
1617
await esClient.indices.delete({
1718
index

src/scripts/view-data.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const esClient = helper.getESClient()
1111
const modelIndexMapping = {
1212
Job: 'ES_INDEX_JOB',
1313
JobCandidate: 'ES_INDEX_JOB_CANDIDATE',
14-
ResourceBooking: 'ES_INDEX_RESOURCE_BOOKING'
14+
ResourceBooking: 'ES_INDEX_RESOURCE_BOOKING',
15+
WorkPeriod: 'ES_INDEX_WORK_PERIOD'
1516
}
1617

1718
async function showESData () {
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* WorkPeriod Processor Service
3+
*/
4+
5+
const Joi = require('@hapi/joi')
6+
const logger = require('../common/logger')
7+
const helper = require('../common/helper')
8+
const constants = require('../common/constants')
9+
const config = require('config')
10+
11+
const esClient = helper.getESClient()
12+
13+
/**
14+
* Process create entity message
15+
* @param {Object} message the kafka message
16+
* @param {String} transactionId
17+
*/
18+
async function processCreate (message, transactionId) {
19+
const workPeriod = message.payload
20+
await esClient.createExtra({
21+
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
22+
id: workPeriod.id,
23+
transactionId,
24+
body: workPeriod,
25+
refresh: constants.esRefreshOption
26+
})
27+
}
28+
29+
processCreate.schema = {
30+
message: Joi.object().keys({
31+
topic: Joi.string().required(),
32+
originator: Joi.string().required(),
33+
timestamp: Joi.date().required(),
34+
'mime-type': Joi.string().required(),
35+
payload: Joi.object().keys({
36+
id: Joi.string().uuid().required(),
37+
resourceBookingId: Joi.string().uuid().required(),
38+
userHandle: Joi.string().required(),
39+
projectId: Joi.number().integer().required(),
40+
startDate: Joi.string().required(),
41+
endDate: Joi.string().required(),
42+
daysWorked: Joi.number().integer().min(0).allow(null),
43+
memberRate: Joi.number().allow(null),
44+
customerRate: Joi.number().allow(null),
45+
paymentStatus: Joi.paymentStatus().required(),
46+
createdAt: Joi.date().required(),
47+
createdBy: Joi.string().uuid().required(),
48+
updatedAt: Joi.date().allow(null),
49+
updatedBy: Joi.string().uuid().allow(null)
50+
}).required()
51+
}).required(),
52+
transactionId: Joi.string().required()
53+
}
54+
55+
/**
56+
* Process update entity message
57+
* @param {Object} message the kafka message
58+
* @param {String} transactionId
59+
*/
60+
async function processUpdate (message, transactionId) {
61+
const data = message.payload
62+
await esClient.updateExtra({
63+
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
64+
id: data.id,
65+
transactionId,
66+
body: {
67+
doc: data
68+
},
69+
refresh: constants.esRefreshOption
70+
})
71+
}
72+
73+
processUpdate.schema = processCreate.schema
74+
75+
/**
76+
* Process delete entity message
77+
* @param {Object} message the kafka message
78+
* @param {String} transactionId
79+
*/
80+
async function processDelete (message, transactionId) {
81+
const id = message.payload.id
82+
await esClient.deleteExtra({
83+
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
84+
id,
85+
transactionId,
86+
refresh: constants.esRefreshOption
87+
})
88+
}
89+
90+
processDelete.schema = {
91+
message: Joi.object().keys({
92+
topic: Joi.string().required(),
93+
originator: Joi.string().required(),
94+
timestamp: Joi.date().required(),
95+
'mime-type': Joi.string().required(),
96+
payload: Joi.object().keys({
97+
id: Joi.string().uuid().required()
98+
}).required()
99+
}).required(),
100+
transactionId: Joi.string().required()
101+
}
102+
103+
module.exports = {
104+
processCreate,
105+
processUpdate,
106+
processDelete
107+
}
108+
109+
logger.buildService(module.exports, 'WorkPeriodProcessorService')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"topic": "taas.workperiod.create",
3+
"originator": "taas-api",
4+
"timestamp": "2021-03-30T20:24:17.555Z",
5+
"mime-type": "application/json",
6+
"payload": {
7+
"resourceBookingId": "6cf2edf6-4b2c-40ef-96db-e1ddb771fdd3",
8+
"startDate": "2021-03-14",
9+
"endDate": "2021-03-20",
10+
"daysWorked": 3,
11+
"memberRate": 13.13,
12+
"customerRate": 13.13,
13+
"paymentStatus": "cancelled",
14+
"projectId": 111,
15+
"userHandle": "pshah_manager",
16+
"id": "926040c4-1709-4de2-b2b6-52adf6e5e72d",
17+
"createdBy": "00000000-0000-0000-0000-000000000000",
18+
"updatedAt": "2021-03-30T20:24:17.541Z",
19+
"createdAt": "2021-03-30T20:24:17.541Z",
20+
"updatedBy": null
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"topic": "taas.workperiod.delete",
3+
"originator": "taas-api",
4+
"timestamp": "2021-03-30T20:13:58.491Z",
5+
"mime-type": "application/json",
6+
"payload": {
7+
"id": "926040c4-1709-4de2-b2b6-52adf6e5e72d"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"topic": "taas.workperiod.update",
3+
"originator": "taas-api",
4+
"timestamp": "2021-03-30T20:13:53.179Z",
5+
"mime-type": "application/json",
6+
"payload": {
7+
"id": "926040c4-1709-4de2-b2b6-52adf6e5e72d",
8+
"resourceBookingId": "79317ff6-5b30-45c2-ace8-b97282b042a8",
9+
"startDate": "2021-03-14",
10+
"endDate": "2021-03-20",
11+
"daysWorked": 3,
12+
"memberRate": 13.13,
13+
"customerRate": 13.13,
14+
"paymentStatus": "pending",
15+
"projectId": 111,
16+
"userHandle": "pshah_manager",
17+
"createdBy": "00000000-0000-0000-0000-000000000000",
18+
"createdAt": "2021-03-30T20:13:34.670Z",
19+
"updatedAt": "2021-03-30T20:13:45.354Z"
20+
}
21+
}

0 commit comments

Comments
 (0)