Skip to content
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
6 changes: 6 additions & 0 deletions src/lib/events/catalog/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { DomainEventMap, EventType } from '../domain-events';
import { codeReportEvents } from './code-report.events';
import { vaultEvents } from './vault.events';
import { projectEvents } from './projects.events';
import { organizationEvents } from './organization.events';
import type {
EventCatalog,
EventDefinition,
Expand All @@ -13,6 +15,8 @@ export type * from './types';
export { OPERATORS } from './fields';
export { CODE_REPORT_TOOLS } from './code-report.events';
export { VAULT_SECRET_CHANGED_FIELDS } from './vault.events';
export { PROJECT_CHANGED_FIELDS } from './projects.events';
export { ORGANIZATION_CHANGED_FIELDS } from './organization.events';

/**
* Builds the catalog. The parameter type makes it exhaustive (every `DomainEventMap` entry needs a
Expand All @@ -35,6 +39,8 @@ export function defineEventCatalog<const C extends EventCatalog>(catalog: C): C
export const EVENT_CATALOG: EventCatalog = defineEventCatalog({
...codeReportEvents,
...vaultEvents,
...projectEvents,
...organizationEvents,
});

export function isEventType(value: string): value is EventType {
Expand Down
25 changes: 25 additions & 0 deletions src/lib/events/catalog/organization.events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { OPERATORS, enumOptions, eventText, fieldLabel } from './fields';
import type { EventDefinitions } from './types';

export const ORGANIZATION_CHANGED_FIELDS = ['name', 'slug', 'description'] as const;

const UPDATED = 'organization.updated';

export const organizationEvents: EventDefinitions<typeof UPDATED> = {
[UPDATED]: {
...eventText(UPDATED),
module: 'organization',
scope: 'organization',
readPermission: 'organization:settings:read',
alertable: true,
fields: [
{
key: 'changedFields',
label: fieldLabel(UPDATED, 'changedFields'),
type: 'enum',
options: enumOptions('organizationChangedField', ORGANIZATION_CHANGED_FIELDS),
operators: OPERATORS.list,
},
],
},
};
75 changes: 75 additions & 0 deletions src/lib/events/catalog/projects.events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { OPERATORS, enumOptions, eventText, fieldLabel } from './fields';
import type { EventDefinitions } from './types';

export const PROJECT_CHANGED_FIELDS = [
'name',
'slug',
'description',
'status',
'modules',
'organization',
'settings',
] as const;

const READ_PROJECTS = 'organization:projects:read';

const CREATED = 'project.created';
const UPDATED = 'project.updated';
const DELETED = 'project.deleted';

function nameField<K extends typeof CREATED | typeof UPDATED | typeof DELETED>(type: K) {
return {
key: 'name',
label: fieldLabel(type, 'name'),
type: 'string',
operators: OPERATORS.string,
} as const;
}

function projectField<K extends typeof UPDATED | typeof DELETED>(type: K) {
return {
key: 'projectId',
label: fieldLabel(type, 'projectId'),
type: 'reference',
reference: 'project',
operators: OPERATORS.reference,
} as const;
}

// projects are watched from their organization: an org admin alerts on its projects
export const projectEvents: EventDefinitions<typeof CREATED | typeof UPDATED | typeof DELETED> = {
[CREATED]: {
...eventText(CREATED),
module: 'projects',
scope: 'organization',
readPermission: READ_PROJECTS,
alertable: true,
fields: [nameField(CREATED)],
},
[UPDATED]: {
...eventText(UPDATED),
module: 'projects',
scope: 'organization',
readPermission: READ_PROJECTS,
alertable: true,
fields: [
projectField(UPDATED),
nameField(UPDATED),
{
key: 'changedFields',
label: fieldLabel(UPDATED, 'changedFields'),
type: 'enum',
options: enumOptions('projectChangedField', PROJECT_CHANGED_FIELDS),
operators: OPERATORS.list,
},
],
},
[DELETED]: {
...eventText(DELETED),
module: 'projects',
scope: 'organization',
readPermission: READ_PROJECTS,
alertable: true,
fields: [projectField(DELETED), nameField(DELETED)],
},
};
26 changes: 26 additions & 0 deletions src/lib/events/domain-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ export type VaultEnvironmentPayload = {
slug: string;
};

export type ProjectChangedField =
'name' | 'slug' | 'description' | 'status' | 'modules' | 'organization' | 'settings';

export type ProjectPayload = {
projectId: string;
name: string;
slug: string;
};

export type ProjectUpdatedPayload = ProjectPayload & {
changedFields: ProjectChangedField[];
};

export type OrganizationChangedField = 'name' | 'slug' | 'description';

export type OrganizationUpdatedPayload = {
organizationId: string;
name: string;
slug: string;
changedFields: OrganizationChangedField[];
};

export interface DomainEventMap {
'code_report.analysis.completed': CodeReportAnalysisCompletedPayload;
'code_report.analysis.failed': CodeReportAnalysisFailedPayload;
Expand All @@ -111,6 +133,10 @@ export interface DomainEventMap {
'vault.env.imported': VaultEnvImportedPayload;
'vault.environment.created': VaultEnvironmentPayload;
'vault.environment.deleted': VaultEnvironmentPayload;
'project.created': ProjectPayload;
'project.updated': ProjectUpdatedPayload;
'project.deleted': ProjectPayload;
'organization.updated': OrganizationUpdatedPayload;
}

export type EventType = Extract<keyof DomainEventMap, string>;
49 changes: 49 additions & 0 deletions src/lib/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,20 @@
"description": "Description",
"folder": "Folder",
"values": "Values"
},
"projectChangedField": {
"name": "Name",
"slug": "Slug",
"description": "Description",
"status": "Status",
"modules": "Modules",
"organization": "Organization",
"settings": "Settings"
},
"organizationChangedField": {
"name": "Name",
"slug": "Slug",
"description": "Description"
}
},
"vault": {
Expand Down Expand Up @@ -1105,6 +1119,41 @@
}
}
}
},
"project": {
"created": {
"label": "Project created",
"description": "A project was created in the organization.",
"fields": {
"name": "Name"
}
},
"updated": {
"label": "Project updated",
"description": "A project of the organization was modified.",
"fields": {
"projectId": "Project",
"name": "Name",
"changedFields": "Changed fields"
}
},
"deleted": {
"label": "Project deleted",
"description": "A project was deleted from the organization.",
"fields": {
"projectId": "Project",
"name": "Name"
}
}
},
"organization": {
"updated": {
"label": "Organization updated",
"description": "The organization details were modified.",
"fields": {
"changedFields": "Changed fields"
}
}
}
}
}
49 changes: 49 additions & 0 deletions src/lib/i18n/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,20 @@
"description": "Descripción",
"folder": "Carpeta",
"values": "Valores"
},
"projectChangedField": {
"name": "Nombre",
"slug": "Slug",
"description": "Descripción",
"status": "Estado",
"modules": "Módulos",
"organization": "Organización",
"settings": "Ajustes"
},
"organizationChangedField": {
"name": "Nombre",
"slug": "Slug",
"description": "Descripción"
}
},
"vault": {
Expand Down Expand Up @@ -1105,6 +1119,41 @@
}
}
}
},
"project": {
"created": {
"label": "Proyecto creado",
"description": "Se ha creado un proyecto en la organización.",
"fields": {
"name": "Nombre"
}
},
"updated": {
"label": "Proyecto actualizado",
"description": "Se ha modificado un proyecto de la organización.",
"fields": {
"projectId": "Proyecto",
"name": "Nombre",
"changedFields": "Campos modificados"
}
},
"deleted": {
"label": "Proyecto eliminado",
"description": "Se ha eliminado un proyecto de la organización.",
"fields": {
"projectId": "Proyecto",
"name": "Nombre"
}
}
},
"organization": {
"updated": {
"label": "Organización actualizada",
"description": "Se han modificado los datos de la organización.",
"fields": {
"changedFields": "Campos modificados"
}
}
}
}
}
39 changes: 36 additions & 3 deletions src/modules/organization/application/organization.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, beforeEach } from 'vitest';
import { OrganizationService } from './organization.service';
import { OrganizationDomain } from '../domain/organization.domain';
import { RecordingEventBus } from '#lib/server/events/testing';

class FakeOrganizationRepository {
rows: OrganizationDomain[] = [];
Expand Down Expand Up @@ -84,9 +85,7 @@ describe('OrganizationService', () => {

describe('createOrganization', () => {
it('requires a non-empty name', async () => {
await expect(service.createOrganization({ name: ' ' })).rejects.toThrow(
/name is required/,
);
await expect(service.createOrganization({ name: ' ' })).rejects.toThrow(/name is required/);
});

it('auto-generates a normalized slug from the name when none is provided', async () => {
Expand Down Expand Up @@ -179,3 +178,37 @@ describe('OrganizationService', () => {
});
});
});

describe('OrganizationService events', () => {
let repository: FakeOrganizationRepository;
let events: RecordingEventBus;
let service: OrganizationService;

beforeEach(async () => {
repository = new FakeOrganizationRepository();
events = new RecordingEventBus();
service = new OrganizationService(repository as any, events);
await repository.create({ id: 'org-1', slug: 'kettu', name: 'Kettu' });
});

it('publishes organization.updated with the changed fields', async () => {
await service.updateOrganization('org-1', { name: 'Kettu', description: 'Software studio' });

expect(events.types()).toEqual(['organization.updated']);
const [event] = events.ofType('organization.updated');
expect(event.payload).toEqual({
organizationId: 'org-1',
name: 'Kettu',
slug: 'kettu',
changedFields: ['description'],
});
expect(event.scope).toEqual({ organizationId: 'org-1', projectId: null });
});

it('publishes nothing when the update is rejected', async () => {
await expect(service.updateOrganization('missing', { name: 'X' })).rejects.toThrow();
await expect(service.updateOrganization('org-1', { name: ' ' })).rejects.toThrow();

expect(events.events).toEqual([]);
});
});
Loading
Loading