feat: reworked ttn webhook endpoint logic
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user