Skip to content
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

Haff/change dispatch to send (#46) #61

Merged
merged 1 commit into from
Feb 28, 2024
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
2 changes: 1 addition & 1 deletion packages/event-producer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tidal-music/event-producer",
"version": "1.1.3",
"version": "1.2.0",
"type": "module",
"files": [
"dist"
Expand Down
14 changes: 7 additions & 7 deletions packages/event-producer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ import { IllegalArgumentError } from '@tidal-music/common';
import * as bus from './bus';
import type { Config } from './config';
import { getConfig } from './config';
import * as dispatch from './dispatch/dispatch';
import { init as _init } from './init';
import * as monitor from './monitor';
import * as outage from './outage';
import * as queue from './queue/queue';
import * as send from './send/send';
import { submitEvents } from './submit/submit';
import type { DispatchedEvent } from './types';
import type { SentEvent } from './types';

export { setConsentCategory, setCredentialsProvider } from './config';
export type * from './types';

/**
* This is the user exposed function that wraps dispatchEvent with the config and credentialsProvider.
* This is the user exposed function that wraps sendEvent with the config and credentialsProvider.
*
* @param {DispatchedEvent} event The event to add to the queue
* @param {SentEvent} event The event to add to the queue
*/
// TODO: error handling.
export const dispatchEvent = (event: DispatchedEvent) => {
export const sendEvent = (event: SentEvent) => {
const config = getConfig();
const { credentialsProvider } = config;
if (credentialsProvider) {
dispatch
.dispatchEvent({
send
.sendEvent({
config,
credentialsProvider,
event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import * as monitor from '../monitor';
import * as queue from '../queue';
import * as uuid from '../uuid/uuid';

import { type DispatchEventParams, dispatchEvent } from './dispatch';
import { type SendEventParams, sendEvent } from './send';

vi.mock('../monitor');
vi.mock('../queue');
vi.mock('@tidal-music/true-time', () => ({
trueTime: { now: vi.fn(() => 1337) },
}));

describe.sequential('dispatchEvent', () => {
describe.sequential('sendEvent', () => {
beforeEach(async () => {
await uuid.init();
});
it('if event consentCategory is not blocked -> adds event to queue', async () => {
vi.spyOn(uuid, 'uuid').mockReturnValue('fakeUuid');
const dispatchEventPayload: DispatchEventParams = {
const sendEventPayload: SendEventParams = {
config,
credentialsProvider: credentialsProvider1,
event: eventPayload1,
};
await dispatchEvent(dispatchEventPayload);
await sendEvent(sendEventPayload);
const { consentCategory, ...eventWithoutConsentCategory } =
dispatchEventPayload.event;
sendEventPayload.event;
expect(queue.addEvent).toHaveBeenCalledWith(
expect.objectContaining({
payload: JSON.stringify({
Expand All @@ -41,15 +41,15 @@ describe.sequential('dispatchEvent', () => {
});

it('if event consentCategory is blocked -> monitor called', async () => {
const dispatchEventPayload: DispatchEventParams = {
const sendEventPayload: SendEventParams = {
config,
credentialsProvider: credentialsProvider1,
event: {
...eventPayload1,
consentCategory: 'TARGETING',
},
};
await dispatchEvent(dispatchEventPayload);
await sendEvent(sendEventPayload);

expect(queue.addEvent).not.toHaveBeenCalled();
expect(monitor.registerDroppedEvent).toHaveBeenCalledOnce();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { trueTime } from '@tidal-music/true-time';
import type { Config } from '../config';
import * as monitor from '../monitor';
import * as queue from '../queue';
import type { DispatchedEvent, EPEvent } from '../types';
import type { EPEvent, SentEvent } from '../types';
import { getEventHeaders } from '../utils/headerUtils';
import { validateEvent } from '../utils/validateEvent';
import { uuid } from '../uuid/uuid';

type CreatePayloadParams = {
event: DispatchedEvent;
event: SentEvent;
id: string;
ts: string;
};
Expand All @@ -30,15 +30,15 @@ const createPayload = ({ event, id, ts }: CreatePayloadParams): string => {
/**
* Creates an EPEvent to be sent to the event producer
*
* @param {DispatchEventParams} params
* @param {SendEventParams} params
*
* @returns {Promise<EPEvent>}
*/
const createEvent = async ({
config,
credentialsProvider,
event,
}: DispatchEventParams): Promise<EPEvent> => {
}: SendEventParams): Promise<EPEvent> => {
const id = uuid();
const sentTimestamp = trueTime.now().toString();
const headers = getEventHeaders({
Expand All @@ -62,7 +62,7 @@ const createEvent = async ({
};

/* c8 ignore start debug only */
export const strictEventCheck = (event: DispatchedEvent) => {
export const strictEventCheck = (event: SentEvent) => {
if (!event.payload) {
throw new Error(`Event is missing payload!: ${JSON.stringify(event)}`);
}
Expand All @@ -77,40 +77,40 @@ export const strictEventCheck = (event: DispatchedEvent) => {
};
/* c8 ignore stop */

export type DispatchEventParams = {
export type SendEventParams = {
config: Config;
credentialsProvider: CredentialsProvider;
event: DispatchedEvent;
event: SentEvent;
};

/**
* Receives an event, validates it, converts it to an EPEvent, and adds it to the queue
*
* @param {DispatchEventParams} params
* @param {SendEventParams} params
*
* @returns {Promise<void | EPEvent[]>}
*/
export const dispatchEvent = async ({
export const sendEvent = async ({
config,
credentialsProvider,
event: dispatchedEvent,
}: DispatchEventParams): Promise<Array<EPEvent> | void> => {
event: sentEvent,
}: SendEventParams): Promise<Array<EPEvent> | void> => {
/* c8 ignore start debug only */
if (config.strictMode) {
strictEventCheck(dispatchedEvent);
strictEventCheck(sentEvent);
}
/* c8 ignore stop */
if (config.blockedConsentCategories[dispatchedEvent.consentCategory]) {
if (config.blockedConsentCategories[sentEvent.consentCategory]) {
monitor.registerDroppedEvent({
eventName: dispatchedEvent.name,
eventName: sentEvent.name,
reason: 'consentFilteredEvents',
});
return Promise.resolve();
}
const event = await createEvent({
config,
credentialsProvider,
event: dispatchedEvent,
event: sentEvent,
});

if (validateEvent(event)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/event-producer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type EventHeaders = Record<string, string>;
/**
* This is an incoming raw event.
*/
export type DispatchedEvent = {
export type SentEvent = {
consentCategory: ConsentCategory;
headers?: EventHeaders;
name: string;
Expand All @@ -23,7 +23,7 @@ export type DispatchedEvent = {
/**
* This is an outgoing prepared event.
*/
export type EPEvent = Omit<DispatchedEvent, 'consentCategory' | 'payload'> & {
export type EPEvent = Omit<SentEvent, 'consentCategory' | 'payload'> & {
id: string;
payload: string;
};
Expand Down
13 changes: 6 additions & 7 deletions packages/event-producer/test/demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { init } from '../../src';
import type { EPEvent } from '../../src';
import { dispatchEvent } from '../../src/dispatch/dispatch';
import { sendEvent } from '../../src/send/send';
import { config as configFixture } from '../fixtures/config';
import {
credentials1,
Expand Down Expand Up @@ -33,7 +33,7 @@ class EventDemo extends HTMLElement {
render() {
this.shadow.innerHTML = `<div><h1></h1><ul>
<li><input type="text" placeholder="credentials token" id="credentialsTokenInp"/><button id="setCredentialsToken">Set credentials token</button><button id="deleteCredentialsToken">Delete token</button></li>
<li><button id="dispatchEventBtn">Fire Event</button> <input id="inp" type="text" placeholder="event name"/></li>
<li><button id="sendEventBtn">Fire Event</button> <input id="inp" type="text" placeholder="event name"/></li>
<li>${this.fakeQueue
.map(
({ id, name }) =>
Expand All @@ -42,8 +42,7 @@ class EventDemo extends HTMLElement {
.join('</li><li>')}</li>
</ul><button id="submitEvents">Submit queue</button></div>`;
if (this.shadowRoot) {
const dispatchEventBtn =
this.shadowRoot.querySelector('#dispatchEventBtn');
const sendEventBtn = this.shadowRoot.querySelector('#sendEventBtn');
const setCredentialsTokenBtn =
this.shadowRoot.querySelector<HTMLButtonElement>(
'#setCredentialsToken',
Expand All @@ -58,7 +57,7 @@ class EventDemo extends HTMLElement {
const submitEventsBtn = this.shadowRoot.querySelector('#submitEvents');
const removeBtns = this.shadowRoot.querySelectorAll('[role="remove"]');
if (
dispatchEventBtn &&
sendEventBtn &&
inp &&
submitEventsBtn &&
setCredentialsTokenBtn &&
Expand All @@ -72,8 +71,8 @@ class EventDemo extends HTMLElement {
console.error('credentials token input is empty');
}
});
dispatchEventBtn.addEventListener('click', () => {
dispatchEvent({
sendEventBtn.addEventListener('click', () => {
sendEvent({
config: configFixture,
credentialsProvider: credentialsProvider1,
event: {
Expand Down
4 changes: 2 additions & 2 deletions packages/event-producer/test/fixtures/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DispatchedEvent, EPEvent } from '../../src';
import type { EPEvent, SentEvent } from '../../src';

export const eventPayload1: DispatchedEvent = {
export const eventPayload1: SentEvent = {
consentCategory: 'NECESSARY',
name: 'display_page',
payload: {
Expand Down
Loading