feat: reworked ttn webhook endpoint logic

This commit is contained in:
2025-01-03 02:01:55 +01:00
parent ffdb644700
commit 62847f569d
17 changed files with 273 additions and 356 deletions

View File

@ -1,7 +1,26 @@
import { inject, injectable } from "tsyringe";
import { TtnGatewayReception } from "../models/ttnGatewayReception";
import { TtnGatewayReceptionRepository } from "../repositories/ttnGatewayReceptionRepository";
interface CreateTtnGatewayReceptionParams {
lp_ttn_end_device_uplinks_id: string;
gateway_id: string;
eui?: string;
rssi?: number;
latitude?: number;
longitude?: number;
altitude?: number;
}
interface UpdateTtnGatewayReceptionParams {
ttn_gateway_reception_id: string;
gateway_id?: string;
eui?: string;
rssi?: number;
latitude?: number;
longitude?: number;
altitude?: number;
}
@injectable()
export class TtnGatewayReceptionService {
constructor(
@ -17,19 +36,24 @@ export class TtnGatewayReceptionService {
return this.repository.findById(id);
}
public async createGatewayReception(data: Partial<TtnGatewayReception>) {
return this.repository.create(data);
}
public async createGatewayReceptions(data: Partial<TtnGatewayReception>[]) {
return this.repository.createMany(data);
}
public async updateGatewayReception(
id: string,
data: Partial<TtnGatewayReception>
public async createTtnGatewayReception(
data: CreateTtnGatewayReceptionParams
) {
return this.repository.update(id, data);
if (data.latitude !== undefined && data.longitude !== undefined)
return this.repository.create(data);
}
public async filterAndInsertGatewayReception(
data: CreateTtnGatewayReceptionParams[]
) {
const result = await Promise.all(
data.map(async (gateway) => await this.createTtnGatewayReception(gateway))
);
return result.filter((gateway) => gateway !== undefined);
}
public async updateGatewayReception(data: UpdateTtnGatewayReceptionParams) {
return this.repository.update(data.ttn_gateway_reception_id, data);
}
public async deleteGatewayReception(id: string) {