Skip to content

Commit

Permalink
feat: more audit events (#364)
Browse files Browse the repository at this point in the history
* added headers to close settlement window function call

* fixed headers to settlement close for eventsdk and added more audit events
  • Loading branch information
vgenev authored Jul 16, 2021
1 parent 3771515 commit acf5d5b
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "central-settlement",
"description": "Central settlements hosted by a scheme to record and make settlements.",
"version": "13.3.2",
"version": "13.4.0",
"license": "Apache-2.0",
"private": false,
"author": "ModusBox",
Expand Down
14 changes: 14 additions & 0 deletions src/api/handlers/settlementWindows.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

const ErrorHandler = require('@mojaloop/central-services-error-handling')
const settlementWindows = require('../../domain/settlementWindow/index')
const Utility = require('@mojaloop/central-services-shared').Util
const Enum = require('@mojaloop/central-services-shared').Enum
const EventSdk = require('@mojaloop/event-sdk')

/**
* Operations on /settlementWindows
Expand All @@ -48,6 +51,17 @@ module.exports = {
*/
get: async function getSettlementWindowsByParams (request, h) {
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT_WINDOW,
Enum.Events.Event.Action.GET,
undefined,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)

const Enums = await request.server.methods.enums('settlementWindowStates')
const settlementWindowResult = await settlementWindows.getByParams({ query: request.query }, Enums)
return h.response(settlementWindowResult)
Expand Down
25 changes: 24 additions & 1 deletion src/api/handlers/settlementWindows/{id}.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

const settlementWindow = require('../../../domain/settlementWindow/index')
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Utility = require('@mojaloop/central-services-shared').Util
const Enum = require('@mojaloop/central-services-shared').Enum
const EventSdk = require('@mojaloop/event-sdk')

/**
* Operations on /settlementWindows/{id}
Expand All @@ -48,6 +51,16 @@ module.exports = {
get: async function getSettlementWindowById (request, h) {
const settlementWindowId = request.params.id
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT_WINDOW,
Enum.Events.Event.Action.GET,
`settlementWindowId=${settlementWindowId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)
const Enums = await request.server.methods.enums('settlementWindowStates')
const settlementWindowResult = await settlementWindow.getById({ settlementWindowId }, Enums, request.server.log)
return h.response(settlementWindowResult)
Expand All @@ -67,8 +80,18 @@ module.exports = {
const { reason } = request.payload
const settlementWindowId = request.params.id
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT_WINDOW,
Enum.Events.Event.Action.POST,
`settlementWindowId=${settlementWindowId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)
const Enums = await request.server.methods.enums('settlementWindowStates')
return await settlementWindow.process({ settlementWindowId, reason, request }, Enums)
return await settlementWindow.process({ settlementWindowId, reason, headers: request.raw.req.headers }, Enums)
} catch (err) {
request.server.log('error', err)
return ErrorHandler.Factory.reformatFSPIOPError(err)
Expand Down
26 changes: 26 additions & 0 deletions src/api/handlers/settlements.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Settlements = require('../../domain/settlement/index')
const Utility = require('@mojaloop/central-services-shared').Util
const Enum = require('@mojaloop/central-services-shared').Enum
const EventSdk = require('@mojaloop/event-sdk')

/**
* Operations on /settlements
Expand All @@ -48,6 +51,17 @@ module.exports = {
*/
get: async function getSettlementsByParams (request, h) {
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.GET,
undefined,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)

const Enums = await request.server.methods.enums('settlementStates')
const settlementResult = await Settlements.getSettlementsByParams({ query: request.query }, Enums)
return h.response(settlementResult)
Expand All @@ -63,8 +77,20 @@ module.exports = {
* produces: application/json
* responses: 200, 400, 401, 404, 415, default
*/

post: async function createSettlementEvent (request, h) {
try {
const { span, payload, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.POST,
payload.settlementWindows.map(id => id.id).join(''),
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)

const Enums = {
ledgerEntryTypes: await request.server.methods.enums('ledgerEntryTypes'),
settlementDelay: await request.server.methods.enums('settlementDelay'),
Expand Down
25 changes: 25 additions & 0 deletions src/api/handlers/settlements/{id}.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Logger = require('@mojaloop/central-services-logger')
const Settlements = require('../../../domain/settlement/index')
const Utility = require('@mojaloop/central-services-shared').Util
const Enum = require('@mojaloop/central-services-shared').Enum
const EventSdk = require('@mojaloop/event-sdk')

/**
* Operations on /settlements/{id}
Expand All @@ -50,6 +53,17 @@ module.exports = {
get: async function getSettlementById (request, h) {
const settlementId = request.params.id
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.GET,
`sid=${settlementId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)

const Enums = await request.server.methods.enums('settlementStates')
request.server.log('info', `get settlement by Id requested with id ${settlementId}`)
const settlementResult = await Settlements.getById({ settlementId }, Enums)
Expand All @@ -70,6 +84,17 @@ module.exports = {
put: async function updateSettlementById (request) {
const settlementId = request.params.id
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.PUT,
`sid=${settlementId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)

const p = request.payload
if (p.participants && (p.state || p.reason || p.externalReference)) {
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, 'No other properties are allowed when participants is provided')
Expand Down
27 changes: 25 additions & 2 deletions src/api/handlers/settlements/{sid}/participants/{pid}.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Settlements = require('../../../../../domain/settlement/index')
const Utility = require('@mojaloop/central-services-shared').Util
const Enum = require('@mojaloop/central-services-shared').Enum
const EventSdk = require('@mojaloop/event-sdk')

/**
* Operations on /settlements/{settlementId}/participants/{participantId}
Expand All @@ -49,12 +52,22 @@ module.exports = {

get: async function getSettlementBySettlementParticipantAccount (request, h) {
try {
const settlementId = request.params.sid
const participantId = request.params.pid
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.GET,
`sid=${settlementId};pid=${participantId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)
const Enums = {
settlementWindowStates: await request.server.methods.enums('settlementWindowStates'),
ledgerAccountTypes: await request.server.methods.enums('ledgerAccountTypes')
}
const settlementId = request.params.sid
const participantId = request.params.pid
const result = await Settlements.getByIdParticipantAccount({ settlementId, participantId }, Enums)
return h.response(result)
} catch (err) {
Expand All @@ -74,6 +87,16 @@ module.exports = {
const settlementId = request.params.sid
const participantId = request.params.pid
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.PUT,
`sid=${settlementId};pid=${participantId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)
const p = request.payload
const universalPayload = {
participants: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

const ErrorHandler = require('@mojaloop/central-services-error-handling')
const Settlements = require('../../../../../../../domain/settlement/index')
const Utility = require('@mojaloop/central-services-shared').Util
const Enum = require('@mojaloop/central-services-shared').Enum
const EventSdk = require('@mojaloop/event-sdk')

/**
* Operations on /settlements/{settlementId}/participants/{participantId}/accounts/{accountId}
Expand All @@ -43,13 +46,23 @@ module.exports = {

get: async function getSettlementBySettlementParticipantAccount (request, h) {
try {
const settlementId = request.params.sid
const participantId = request.params.pid
const accountId = request.params.aid
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.GET,
`sid=${settlementId};pid=${participantId};aid=${accountId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)
const Enums = {
settlementWindowStates: await request.server.methods.enums('settlementWindowStates'),
ledgerAccountTypes: await request.server.methods.enums('ledgerAccountTypes')
}
const settlementId = request.params.sid
const participantId = request.params.pid
const accountId = request.params.aid
const result = await Settlements.getByIdParticipantAccount({ settlementId, participantId, accountId }, Enums)
return h.response(result)
} catch (err) {
Expand All @@ -70,6 +83,16 @@ module.exports = {
const participantId = request.params.pid
const accountId = request.params.aid
try {
const { span, headers } = request
const spanTags = Utility.EventFramework.getSpanTags(
Enum.Events.Event.Type.SETTLEMENT,
Enum.Events.Event.Action.PUT,
`sid=${settlementId};pid=${participantId};aid=${accountId}`,
headers[Enum.Http.Headers.FSPIOP.SOURCE],
headers[Enum.Http.Headers.FSPIOP.DESTINATION]
)
span.setTags(spanTags)
await span.audit(request.payload, EventSdk.AuditEventAction.ingress)
const accounts = [Object.assign({}, request.payload, { id: accountId })]
const universalPayload = {
participants: [
Expand Down
3 changes: 1 addition & 2 deletions src/domain/settlementWindow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ module.exports = {

process: async function (params, enums) {
const settlementWindowId = await SettlementWindowModel.process(params, enums)

const messageId = Uuid()
const eventId = Uuid()
const state = StreamingProtocol.createEventState(Enum.Events.EventStatus.SUCCESS.status, Enum.Events.EventStatus.SUCCESS.code, Enum.Events.EventStatus.SUCCESS.description)
const event = StreamingProtocol.createEventMetadata(Enum.Events.Event.Type.DEFERRED_SETTLEMENT, Enum.Events.Event.Action.CLOSE, state)
const metadata = StreamingProtocol.createMetadata(eventId, event)
const messageProtocol = StreamingProtocol.createMessage(messageId, Enum.Http.Headers.FSPIOP.SWITCH.value, Enum.Http.Headers.FSPIOP.SWITCH.value, metadata, params.request.headers, params)
const messageProtocol = StreamingProtocol.createMessage(messageId, Enum.Http.Headers.FSPIOP.SWITCH.value, Enum.Http.Headers.FSPIOP.SWITCH.value, metadata, params.headers, params)
const topicConfig = KafkaUtil.createGeneralTopicConf(Config.KAFKA_CONFIG.TOPIC_TEMPLATES.GENERAL_TOPIC_TEMPLATE.TEMPLATE, Enum.Events.Event.Type.DEFERRED_SETTLEMENT, Enum.Events.Event.Action.CLOSE)
const kafkaConfig = KafkaUtil.getKafkaConfig(Config.KAFKA_CONFIG, Enum.Kafka.Config.PRODUCER, Enum.Events.Event.Type.DEFERRED_SETTLEMENT.toUpperCase(), Enum.Events.Event.Action.CLOSE.toUpperCase())
await Producer.produceMessage(messageProtocol, topicConfig, kafkaConfig)
Expand Down
13 changes: 12 additions & 1 deletion src/interface/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
}
],
"tags": [
"getSettlementWindowById"
"getSettlementWindowById",
"sampled"
],
"responses": {
"200": {
Expand Down Expand Up @@ -122,6 +123,7 @@
}
],
"tags": [
"sampled",
"closeSettlementWindow"
],
"responses": {
Expand Down Expand Up @@ -221,6 +223,7 @@

],
"tags": [
"sampled",
"getSettlementWindowsByParams"
],
"responses": {
Expand Down Expand Up @@ -351,6 +354,7 @@
}
],
"tags": [
"sampled",
"getSettlementsByParams"
],
"responses": {
Expand Down Expand Up @@ -415,6 +419,7 @@
}
],
"tags": [
"sampled",
"createSettlement"
],
"responses": {
Expand Down Expand Up @@ -476,6 +481,7 @@
}
],
"tags": [
"sampled",
"getSettlementById"
],
"responses": {
Expand Down Expand Up @@ -547,6 +553,7 @@
}
],
"tags": [
"sampled",
"updateSettlementById"
],
"responses": {
Expand Down Expand Up @@ -615,6 +622,7 @@
}
],
"tags": [
"sampled",
"getSettlementBySettlementParticipant"
],
"responses": {
Expand Down Expand Up @@ -693,6 +701,7 @@
}
],
"tags": [
"sampled",
"updateSettlementBySettlementParticipant"
],
"responses": {
Expand Down Expand Up @@ -768,6 +777,7 @@
}
],
"tags": [
"sampled",
"getSettlementBySettlementParticipantAccount"
],
"responses": {
Expand Down Expand Up @@ -853,6 +863,7 @@
}
],
"tags": [
"sampled",
"updateSettlementBySettlementParticipantAccount"
],
"responses": {
Expand Down

0 comments on commit acf5d5b

Please sign in to comment.