Skip to content

Commit

Permalink
hack: external events
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois committed Dec 5, 2023
1 parent fa9d38f commit 419a8c5
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/lib/db/event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ export interface IEventTable {
const TABLE = 'events';

class EventStore implements IEventStore {
private db: Db;
protected db: Db;

// only one shared event emitter should exist across all event store instances
private eventEmitter: EventEmitter = sharedEventEmitter;

private logger: Logger;
protected logger: Logger;

// a new DB has to be injected per transaction
constructor(db: Db, getLogger: LogProvider) {
Expand Down
2 changes: 0 additions & 2 deletions src/lib/openapi/spec/event-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FromSchema } from 'json-schema-to-ts';
import { tagSchema } from './tag-schema';
import { IEventTypes } from '../../types';
import { variantSchema } from './variant-schema';

const eventDataSchema = {
Expand Down Expand Up @@ -44,7 +43,6 @@ export const eventSchema = {
type: 'string',
description:
'What [type](https://docs.getunleash.io/reference/api/legacy/unleash/admin/events#event-type-description) of event this is',
enum: IEventTypes,
example: 'feature-created',
},
createdBy: {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/services/event-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ITag } from '../types';
export default class EventService {
private logger: Logger;

private eventStore: IEventStore;
protected eventStore: IEventStore;

private featureTagStore: IFeatureTagStore;

Expand Down Expand Up @@ -51,7 +51,7 @@ export default class EventService {
return this.eventStore.on(eventName, listener);
}

private async enhanceEventsWithTags(
protected async enhanceEventsWithTags(
events: IBaseEvent[],
): Promise<IBaseEvent[]> {
const featureNamesSet = new Set<string>();
Expand Down
14 changes: 14 additions & 0 deletions src/lib/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export const BANNER_CREATED = 'banner-created' as const;
export const BANNER_UPDATED = 'banner-updated' as const;
export const BANNER_DELETED = 'banner-deleted' as const;

export const EVENT_WEBHOOK_CREATED = 'event-webhook-created' as const;
export const EVENT_WEBHOOK_UPDATED = 'event-webhook-updated' as const;
export const EVENT_WEBHOOK_DELETED = 'event-webhook-deleted' as const;

export const EVENT_ACTION_CREATED = 'event-action-created' as const;
export const EVENT_ACTION_UPDATED = 'event-action-updated' as const;
export const EVENT_ACTION_DELETED = 'event-action-deleted' as const;

export const IEventTypes = [
APPLICATION_CREATED,
FEATURE_CREATED,
Expand Down Expand Up @@ -306,6 +314,12 @@ export const IEventTypes = [
PROJECT_ENVIRONMENT_ADDED,
PROJECT_ENVIRONMENT_REMOVED,
DEFAULT_STRATEGY_UPDATED,
EVENT_WEBHOOK_CREATED,
EVENT_WEBHOOK_UPDATED,
EVENT_WEBHOOK_DELETED,
EVENT_ACTION_CREATED,
EVENT_ACTION_UPDATED,
EVENT_ACTION_DELETED,
] as const;
export type IEventType = (typeof IEventTypes)[number];

Expand Down
19 changes: 19 additions & 0 deletions src/migrations/20231205122703-external-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exports.up = function (db, cb) {
db.runSql(
`
ALTER table events
ADD COLUMN IF NOT EXISTS is_external boolean DEFAULT false
`,
cb,
);
};

exports.down = function (db, cb) {
db.runSql(
`
ALTER table events
DROP COLUMN is_external
`,
cb,
);
};
27 changes: 27 additions & 0 deletions src/migrations/20231205155322-event-webhooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

exports.up = function (db, cb) {
db.runSql(
`
CREATE TABLE IF NOT EXISTS event_webhooks
(
id SERIAL PRIMARY KEY NOT NULL,
enabled BOOLEAN DEFAULT true NOT NULL,
name TEXT NOT NULL,
event TEXT NOT NULL,
url TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
`,
cb,
);
};

exports.down = function (db, cb) {
db.runSql(
`
DROP TABLE IF EXISTS event_webhooks;
`,
cb,
);
};
25 changes: 25 additions & 0 deletions src/migrations/20231205155331-event-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

exports.up = function (db, cb) {
db.runSql(
`
CREATE TABLE IF NOT EXISTS event_actions
(
id SERIAL PRIMARY KEY NOT NULL,
event TEXT NOT NULL,
action TEXT NOT NULL,
parameters JSONB NOT NULL DEFAULT '{}'::jsonb
);
`,
cb,
);
};

exports.down = function (db, cb) {
db.runSql(
`
DROP TABLE IF EXISTS event_actions;
`,
cb,
);
};
2 changes: 1 addition & 1 deletion src/test/fixtures/fake-event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import EventEmitter from 'events';
class FakeEventStore implements IEventStore {
events: IEvent[];

private eventEmitter: EventEmitter = sharedEventEmitter;
protected eventEmitter: EventEmitter = sharedEventEmitter;

constructor() {
this.eventEmitter.setMaxListeners(0);
Expand Down

0 comments on commit 419a8c5

Please sign in to comment.