From 0d87c2ae13f4a956a9a21e842699e79ddf15bfc8 Mon Sep 17 00:00:00 2001 From: Martin Sottnik Date: Wed, 18 Sep 2024 14:23:09 +0200 Subject: [PATCH 1/3] missing geolocation in kafka event fix --- src/schema/device.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/schema/device.ts b/src/schema/device.ts index 22ef518a..e5fc0b7d 100644 --- a/src/schema/device.ts +++ b/src/schema/device.ts @@ -293,9 +293,10 @@ export const AddDeviceMutation = extendType({ resolve: async (_, args, { prisma, tenantId, kafka, inventoryKafka }) => { const { input } = args; const nativeZoneId = fromGraphId('Zone', input.zoneId); + const nativeLocationId = input.locationId ? fromGraphId('Location', input.locationId) : null; const zone = await prisma.uniconfigZone.findFirst({ where: { tenantId, id: nativeZoneId } }); const deviceLocation = await prisma.location.findFirst({ - where: { id: input.locationId ?? undefined }, + where: { id: nativeLocationId ?? undefined }, }); if (zone == null) { From 861bebb8d0585485f484b7efe2076d3da98a03d7 Mon Sep 17 00:00:00 2001 From: Martin Sottnik Date: Wed, 18 Sep 2024 16:08:19 +0200 Subject: [PATCH 2/3] more kafka related fixes --- src/external-api/kafka.ts | 5 +++-- src/helpers/device-helpers.ts | 2 +- src/schema/device.ts | 9 ++++++++- src/test/helpers.ts | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/external-api/kafka.ts b/src/external-api/kafka.ts index 76b99bb8..edab3349 100644 --- a/src/external-api/kafka.ts +++ b/src/external-api/kafka.ts @@ -113,7 +113,8 @@ async function produceDeviceRegistrationEvent( } try { - const kafkaCoordinates: DeviceLocation | null = coordinates ? { type: 'Point', coordinates } : null; + const kafkaCoordinates: DeviceLocation | null = coordinates ? { type: 'POINT', coordinates } : null; + console.log(encodeDeviceForInventoryKafka(device, kafkaCoordinates, labelIds)); await kafka.send(device.name, encodeDeviceForInventoryKafka(device, kafkaCoordinates, labelIds), { type: 'device_registration', }); @@ -153,7 +154,7 @@ async function produceDeviceUpdateEvent( } try { - const kafkaCoordinates: DeviceLocation | null = coordinates ? { type: 'Point', coordinates } : null; + const kafkaCoordinates: DeviceLocation | null = coordinates ? { type: 'POINT', coordinates } : null; await kafka.send(device.name, encodeDeviceForInventoryKafka(device, kafkaCoordinates, labelIds), { type: 'device_update', }); diff --git a/src/helpers/device-helpers.ts b/src/helpers/device-helpers.ts index 75aa2ba7..abdba38a 100644 --- a/src/helpers/device-helpers.ts +++ b/src/helpers/device-helpers.ts @@ -91,7 +91,7 @@ export function makeZonesWithDevicesFromDevices(devices: Device[]) { } export type DeviceLocation = { - type: 'Point'; + type: 'POINT'; coordinates: [number, number]; }; diff --git a/src/schema/device.ts b/src/schema/device.ts index e5fc0b7d..dedb6712 100644 --- a/src/schema/device.ts +++ b/src/schema/device.ts @@ -44,6 +44,7 @@ import { LabelConnection } from './label'; import { Location } from './location'; import { Zone } from './zone'; import config from '../config'; +import { disconnect } from 'node:process'; export const DeviceServiceState = enumType({ name: 'DeviceServiceState', @@ -431,6 +432,8 @@ export const UpdateDeviceMutation = extendType({ ...oldMetadata, deviceSize: input.deviceSize, }; + + console.log('locationId: ', input.locationId); const updatedDevice = await prisma.device.update({ where: { id: nativeId }, data: { @@ -444,7 +447,11 @@ export const UpdateDeviceMutation = extendType({ password: input.password, port: input.port, serviceState: input.serviceState ?? undefined, - location: input.locationId ? { connect: { id: fromGraphId('Location', input.locationId) } } : undefined, + location: input.locationId + ? { connect: { id: fromGraphId('Location', input.locationId) } } + : { + disconnect: true, + }, blueprint: input.blueprintId ? { connect: { id: fromGraphId('Blueprint', input.blueprintId) } } : undefined, diff --git a/src/test/helpers.ts b/src/test/helpers.ts index afdc4a05..60ba6c11 100644 --- a/src/test/helpers.ts +++ b/src/test/helpers.ts @@ -57,7 +57,7 @@ async function produceDeviceRegistrationEventMock( } try { - await kafka.send(device.name, encodeDeviceForInventoryKafka(device, { type: 'Point', coordinates }, labelIds), { + await kafka.send(device.name, encodeDeviceForInventoryKafka(device, { type: 'POINT', coordinates }, labelIds), { type: 'device_registration', }); } catch (error) { @@ -96,7 +96,7 @@ async function produceDeviceUpdateEventMock( } try { - await kafka.send(device.name, encodeDeviceForInventoryKafka(device, { type: 'Point', coordinates }, labelIds), { + await kafka.send(device.name, encodeDeviceForInventoryKafka(device, { type: 'POINT', coordinates }, labelIds), { type: 'device_update', }); } catch (error) { From 724206f4919bdd18b7aa73a86d856e5498793c24 Mon Sep 17 00:00:00 2001 From: Martin Sottnik Date: Wed, 18 Sep 2024 16:10:37 +0200 Subject: [PATCH 3/3] lint fixes --- src/external-api/kafka.ts | 1 - src/schema/device.ts | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/external-api/kafka.ts b/src/external-api/kafka.ts index edab3349..1037301d 100644 --- a/src/external-api/kafka.ts +++ b/src/external-api/kafka.ts @@ -114,7 +114,6 @@ async function produceDeviceRegistrationEvent( try { const kafkaCoordinates: DeviceLocation | null = coordinates ? { type: 'POINT', coordinates } : null; - console.log(encodeDeviceForInventoryKafka(device, kafkaCoordinates, labelIds)); await kafka.send(device.name, encodeDeviceForInventoryKafka(device, kafkaCoordinates, labelIds), { type: 'device_registration', }); diff --git a/src/schema/device.ts b/src/schema/device.ts index dedb6712..e0c1bdc4 100644 --- a/src/schema/device.ts +++ b/src/schema/device.ts @@ -44,7 +44,6 @@ import { LabelConnection } from './label'; import { Location } from './location'; import { Zone } from './zone'; import config from '../config'; -import { disconnect } from 'node:process'; export const DeviceServiceState = enumType({ name: 'DeviceServiceState', @@ -433,7 +432,6 @@ export const UpdateDeviceMutation = extendType({ deviceSize: input.deviceSize, }; - console.log('locationId: ', input.locationId); const updatedDevice = await prisma.device.update({ where: { id: nativeId }, data: {