Skip to content

Commit

Permalink
chore: test rule
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 29, 2022
1 parent 380a19d commit 148a0db
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 81 deletions.
4 changes: 2 additions & 2 deletions carpark/event-bridge/eipfs-indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const SQS_INDEXER_QUEUE_URL =
const SQS_INDEXER_QUEUE_REGION = 'us-west-2'

/**
* @param {import('.').EventBridgeEvent} event
* @param {import('./source').EventBridgeEvent} event
* @param {SQSClient} client
* @param {string} queueUrl
*/
Expand All @@ -24,7 +24,7 @@ export async function eipfsHandler(event, client, queueUrl) {
}

/**
* @param {import('.').EventBridgeEvent} event
* @param {import('./source').EventBridgeEvent} event
*/
export async function handler (event) {
const sqsClient = new SQSClient({
Expand Down
4 changes: 2 additions & 2 deletions carpark/event-bridge/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const SQS_REPLICATOR_QUEUE_URL = process.env.SQS_REPLICATOR_QUEUE_URL || ''
const SQS_REPLICATOR_QUEUE_REGION = 'us-west-2'

/**
* @param {import('.').EventBridgeEvent} event
* @param {import('./source').EventBridgeEvent} event
* @param {SQSClient} client
* @param {string} queueUrl
*/
Expand All @@ -22,7 +22,7 @@ export async function replicatorHandler(event, client, queueUrl) {
}

/**
* @param {import('.').EventBridgeEvent} event
* @param {import('./source').EventBridgeEvent} event
*/
export async function handler (event) {
const sqsClient = new SQSClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const CARPARK_EVENT_BRIDGE_SOURCE_EVENT = 'carpark_bucket'
* @param {import('@aws-sdk/client-eventbridge').EventBridge} eventBridge
* @param {string} eventBusName
*/
export async function notifyBusNewCar(event, eventBridge, eventBusName) {
export async function notifyBus(event, eventBridge, eventBusName) {
const entries = event.Records
? event.Records.map((r) => ({
key: r?.s3?.object?.key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { EventBridge } from '@aws-sdk/client-eventbridge'

import { notifyBusNewCar } from '../event-bridge/index.js'
import { notifyBus } from '../event-bridge/source.js'

const CARPARK_BUS_ARN = process.env.CARPARK_BUS_ARN || ''
const EVENT_BUS_ARN = process.env.EVENT_BUS_ARN || ''

/**
* @param {import('aws-lambda').S3Event} event
*/
async function handler(event) {
const bus = new EventBridge({})

return await notifyBusNewCar(event, bus, CARPARK_BUS_ARN)
return await notifyBus(event, bus, EVENT_BUS_ARN)
}

export const carparkBucketConsumer = handler
6 changes: 3 additions & 3 deletions carpark/test/event-bridge.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from './helpers/context.js'

import { notifyBusNewCar } from '../event-bridge/index.js'
import { notifyBus } from '../event-bridge/source.js'
import { eipfsHandler } from '../event-bridge/eipfs-indexer.js'
import { replicatorHandler } from '../event-bridge/replicator.js'

Expand Down Expand Up @@ -33,7 +33,7 @@ test('notifies carpark event bridge when new carpark bucket is written', async t
}
}

const response = await notifyBusNewCar({
const response = await notifyBus({
// @ts-expect-error incomplete S3 event metadata
Records: fixtureS3PutValidRecords
},
Expand All @@ -51,7 +51,7 @@ test('does not notify carpark event bridge when new carpark bucket is written wi
}
}

const response = await notifyBusNewCar({
const response = await notifyBus({
// @ts-expect-error incomplete S3 event metadata
Records: fixtureS3PutInvalidRecords
},
Expand Down
102 changes: 52 additions & 50 deletions package-lock.json

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

File renamed without changes.
36 changes: 36 additions & 0 deletions satnav/event-bridge/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const SATNAV_EVENT_BRIDGE_SOURCE_EVENT = 'carpark_bucket'

/**
* @typedef {{ detail: {key?: string, region: string, bucketName: string}}} EventBridgeEvent
*/

/**
* @param {import('aws-lambda').S3Event} event
* @param {import('@aws-sdk/client-eventbridge').EventBridge} eventBridge
* @param {string} eventBusName
*/
export async function notifyBus(event, eventBridge, eventBusName) {
const entries = event.Records
? event.Records.map((r) => ({
key: r?.s3?.object?.key,
region: r?.awsRegion || 'us-west-2',
bucketName: r?.s3?.bucket?.name,
})).filter((entry) => entry.key)
: []

if (entries.length > 0) {
const feedbackEntries = entries.map((entry) => ({
EventBusName: eventBusName,
Source: SATNAV_EVENT_BRIDGE_SOURCE_EVENT,
DetailType: 'satnav_index_added',
Detail: JSON.stringify(entry),
}))
await eventBridge.putEvents({ Entries: feedbackEntries })
}

return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: `File was added to satnav bucket`,
}
}
16 changes: 16 additions & 0 deletions satnav/functions/satnav-bucket-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { EventBridge } from '@aws-sdk/client-eventbridge'

import { notifyBus } from '../event-bridge/source.js'

const EVENT_BUS_ARN = process.env.EVENT_BUS_ARN || ''

/**
* @param {import('aws-lambda').S3Event} event
*/
async function handler(event) {
const bus = new EventBridge({})

return await notifyBus(event, bus, EVENT_BUS_ARN)
}

export const satnavBucketConsumer = handler
5 changes: 4 additions & 1 deletion satnav/functions/satnav-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function handler (event) {
SATNAV_BUCKET_NAME,
} = getEnv()

console.log('event', event)
const record = parseSqsEvent(event)
if (!record) {
throw new Error('Unexpected sqs record format')
Expand Down Expand Up @@ -46,7 +47,9 @@ export function handler (event) {
*/
function mustGetEnv (name) {
if (!process.env[name]) {
throw new Error(`Missing env var: ${name}`)
// throw new Error(`Missing env var: ${name}`)
console.log('BUCKET', process.env)
process.env[name] = 'pr55-upload-api-satnavst-satnavstorebucketab31648-1u8h96j96is2z'
}

// @ts-expect-error there will always be a string there, but typescript does not believe
Expand Down
Loading

0 comments on commit 148a0db

Please sign in to comment.