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
62 changes: 36 additions & 26 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AssetCategoriesModule } from './asset-categories/asset-categories.module';
import { SettingsModule } from './settings/settings.module';
import { AssetCategory } from './asset-categories/asset-category.entity';
import { DepartmentsModule } from './departments/departments.module';
import { Department } from './departments/department.entity';
import { UsersModule } from './users/users.module';
import { User } from './users/entities/user.entity';
import { SearchModule } from './search/search.module';
import { AuthModule } from './auth/auth.module';
import { RiskModule } from './risk/risk.module';
import { ReportingModule } from './reporting/reporting.module';
import { AssetTransfersModule } from './asset-transfers/asset-transfers.module';
import { FileUpload } from './file-uploads/entities/file-upload.entity';
import { Asset } from './assets/entities/assest.entity';
import { Supplier } from './suppliers/entities/supplier.entity';
import { QrBarcodeModule } from './qr-barcode/qr-barcode.module';
import { VendorContractsModule } from './vendor-contracts/vendor-contracts.module';
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ScheduleModule } from "@nestjs/schedule";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { UsersModule } from "./users/users.module";
import { AuthModule } from "./auth/auth.module";
import { ApiKeysModule } from "./api-keys/api-keys.module";
import { OrganizationUnitsModule } from "./organization-units/organization-units.module";
import { ChangeLogModule } from "./change-log/change-log.module";
import { BarcodeModule } from "./barcode/barcode.module";
import { ComplianceModule } from "./compliance/compliance.module";
import { MobileDevicesModule } from "./mobile-devices/mobile-devices.module";
import { PolicyDocumentsModule } from "./policy-documents/policy-documents.module";
import { DeviceHealthModule } from "./device-health/device-health.module";
import { QRCodeModule } from "./QR-Code/qrcode.module";
import { NotificationsModule } from "./notifications/notifications.module";
import { StatusHistoryModule } from "./status-history/status-history.module";
import { DisposalRegistryModule } from "./disposal-registry/disposal-registry.module";
import { VendorDirectoryModule } from "./vendor-directory/vendor-directory.module";
import { WebhooksModule } from "./webhooks/webhooks.module";

@Module({
imports: [
Expand Down Expand Up @@ -54,10 +54,20 @@ import { VendorContractsModule } from './vendor-contracts/vendor-contracts.modul
UsersModule,
SearchModule,
AuthModule,
RiskModule,
ReportingModule,
QrBarcodeModule,
VendorContractsModule,
ApiKeysModule,
OrganizationUnitsModule,
ChangeLogModule,
BarcodeModule,
ComplianceModule,
MobileDevicesModule,
PolicyDocumentsModule,
DeviceHealthModule,
QRCodeModule,
NotificationsModule,
StatusHistoryModule,
DisposalRegistryModule,
VendorDirectoryModule,
WebhooksModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
14 changes: 14 additions & 0 deletions backend/src/webhooks/assesst/assets.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import { EventEmitter2 } from '@nestjs/event-emitter';

export class AssetsService {
constructor(private repo: any, private eventEmitter: EventEmitter2) {
// Initialization logic if needed
}

createAsset(data) {
const asset = this.repo.save(data);
this.eventEmitter.emit('asset.created', asset);
}
}

10 changes: 10 additions & 0 deletions backend/src/webhooks/dto/create-webhook.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// webhooks/dto/create-webhook.dto.ts
import { IsUrl, IsString } from 'class-validator';

export class CreateWebhookDto {
@IsUrl()
url: string;

@IsString()
eventType: string;
}
17 changes: 17 additions & 0 deletions backend/src/webhooks/entities/webhook.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// webhooks/entities/webhook.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity('webhooks')
export class Webhook {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
url: string;

@Column() // e.g. "asset.created", "asset.updated"
eventType: string;

@Column({ default: true })
isActive: boolean;
}
18 changes: 18 additions & 0 deletions backend/src/webhooks/webhooks.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { WebhooksController } from './webhooks.controller';

describe('WebhooksController', () => {
let controller: WebhooksController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [WebhooksController],
}).compile();

controller = module.get<WebhooksController>(WebhooksController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
19 changes: 19 additions & 0 deletions backend/src/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { Controller, Post, Body, Get } from '@nestjs/common';
import { WebhooksService } from './webhooks.service';
import { CreateWebhookDto } from './dto/create-webhook.dto';

@Controller('webhooks')
export class WebhooksController {
constructor(private webhookService: WebhooksService) {}

@Post()
registerWebhook(@Body() dto: CreateWebhookDto) {
return this.webhookService.register(dto);
}

@Get()
findAll() {
return this.webhookService.findAll();
}
}
14 changes: 14 additions & 0 deletions backend/src/webhooks/webhooks.listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// webhooks/webhooks.listener.ts
import { OnEvent } from '@nestjs/event-emitter';
import { WebhooksService } from './webhooks.service';
import { Injectable } from '@nestjs/common';

@Injectable()
export class WebhookListener {
constructor(private webhookService: WebhooksService) {}

@OnEvent('asset.created')
handleNewAsset(asset) {
this.webhookService.triggerEvent('asset.created', asset);
}
}
13 changes: 13 additions & 0 deletions backend/src/webhooks/webhooks.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { WebhooksController } from './webhooks.controller';
import { WebhooksService } from './webhooks.service';
import { WebhookListener } from './webhooks.listener';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Webhook } from './entities/webhook.entity';

@Module({
imports: [TypeOrmModule.forFeature([Webhook])],
controllers: [WebhooksController],
providers: [WebhooksService, WebhookListener]
})
export class WebhooksModule {}
18 changes: 18 additions & 0 deletions backend/src/webhooks/webhooks.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { WebhooksService } from './webhooks.service';

describe('WebhooksService', () => {
let service: WebhooksService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [WebhooksService],
}).compile();

service = module.get<WebhooksService>(WebhooksService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
33 changes: 33 additions & 0 deletions backend/src/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Webhook } from './entities/webhook.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateWebhookDto } from './dto/create-webhook.dto';
import axios from 'axios';

@Injectable()
export class WebhooksService {
constructor(
@InjectRepository(Webhook) private repo: Repository<Webhook>,
) {}

register(dto: CreateWebhookDto) {
return this.repo.save(dto);
}

findAll() {
return this.repo.find();
}

async triggerEvent(eventType: string, payload: any) {
const webhooks = await this.repo.find({ where: { eventType, isActive: true }});

for (const hook of webhooks) {
try {
await axios.post(hook.url, payload);
} catch (e) {
console.error(`Webhook failed: ${hook.url}`, e.message);
}
}
}
}