From d0114909c23706ee1bca96daf3631e8a3ebe187e Mon Sep 17 00:00:00 2001 From: Rick B Date: Fri, 6 Oct 2023 14:47:43 -0700 Subject: [PATCH] fix: fixed mongo device deletion --- data/initMPS.sql | 1 + src/data/mongo/collections/device.test.ts | 4 +- src/data/mongo/collections/device.ts | 4 +- src/data/postgres/tables/device.test.ts | 106 ++++++++++++++++++---- src/data/postgres/tables/device.ts | 30 +++--- src/models/models.ts | 9 ++ src/routes/devices/create.test.ts | 10 +- src/routes/devices/get.test.ts | 10 +- src/routes/devices/getAll.test.ts | 10 +- 9 files changed, 146 insertions(+), 38 deletions(-) diff --git a/data/initMPS.sql b/data/initMPS.sql index 14c0da311..965cfeb20 100644 --- a/data/initMPS.sql +++ b/data/initMPS.sql @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS devices( tenantid varchar(36) NOT NULL, friendlyname varchar(256), dnssuffix varchar(256), + deviceinfo JSON, CONSTRAINT device_guid UNIQUE(guid), PRIMARY KEY (guid,tenantid) ); diff --git a/src/data/mongo/collections/device.test.ts b/src/data/mongo/collections/device.test.ts index 1e516ae15..52c68a83b 100644 --- a/src/data/mongo/collections/device.test.ts +++ b/src/data/mongo/collections/device.test.ts @@ -67,10 +67,10 @@ describe('MongoDeviceTable', () => { it('should delete document by name and tenantId', async () => { collection.deleteOne.mockResolvedValue({ deletedCount: 1 } as any) - const result = await mongoDeviceTable.delete('someName', 'someTenantId') + const result = await mongoDeviceTable.delete('someGuid', 'someTenantId') expect(result).toBe(true) - expect(collection.deleteOne).toHaveBeenCalledWith({ friendlyName: 'someName', tenantId: 'someTenantId' }) + expect(collection.deleteOne).toHaveBeenCalledWith({ guid: 'someGuid', tenantId: 'someTenantId' }) }) it('should insert a device', async () => { diff --git a/src/data/mongo/collections/device.ts b/src/data/mongo/collections/device.ts index 8374fb2f4..40c3b3d26 100644 --- a/src/data/mongo/collections/device.ts +++ b/src/data/mongo/collections/device.ts @@ -33,8 +33,8 @@ export class MongoDeviceTable implements IDeviceTable { return this.collection.findOne({ guid: id, tenantId }) as unknown as WithId } - async delete (name: string, tenantId: string = ''): Promise { - const result = await this.collection.deleteOne({ friendlyName: name, tenantId }) + async delete (id: string, tenantId: string = ''): Promise { + const result = await this.collection.deleteOne({ guid: id, tenantId }) return result.deletedCount && result.deletedCount > 0 } diff --git a/src/data/postgres/tables/device.test.ts b/src/data/postgres/tables/device.test.ts index 231304b2c..30b145500 100644 --- a/src/data/postgres/tables/device.test.ts +++ b/src/data/postgres/tables/device.test.ts @@ -58,7 +58,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tenantid = $3 ORDER BY guid @@ -88,7 +89,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tenantid = $3 ORDER BY guid @@ -111,7 +113,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE guid = $1`, ['4c4c4544-004b-4210-8033-b6c04f504633']) }) @@ -161,7 +164,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE guid = $1 and tenantid = $2`, ['4c4c4544-004b-4210-8033-b6c04f504633', 'tenantId']) }) @@ -190,7 +194,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE hostname = $1 and tenantid = $2`, ['hostname', 'tenantId']) }) @@ -210,7 +215,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE hostname = $1 and tenantid = $2`, ['hostname', '']) }) @@ -264,7 +270,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tags @> $1 and tenantId = $4 ORDER BY guid @@ -295,7 +302,8 @@ describe('device tests', () => { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tags && $1 and tenantId = $4 ORDER BY guid @@ -336,15 +344,23 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtBuild: '1111', + amtSku: '16392', + amtVersion: '16.1', + currentMode: '0', + dnsSuffixOs: '', + ipAddress: '' + } } querySpy.mockResolvedValueOnce({ rows: [{ device }], command: '', fields: null, rowCount: 1, oid: 0 }) getById.mockResolvedValueOnce(device) const result = await deviceTable.insert(device) expect(querySpy).toBeCalledTimes(1) expect(querySpy).toBeCalledWith(` - INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix) - values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9)`, + INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix, deviceinfo) + values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9, $10)`, [ device.guid, device.hostname, @@ -354,7 +370,8 @@ describe('device tests', () => { device.mpsusername, device.tenantId, device.friendlyName, - device.dnsSuffix + device.dnsSuffix, + JSON.stringify(device.deviceInfo) ]) expect(getById).toBeCalledTimes(1) expect(result).toBe(device) @@ -370,7 +387,15 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } querySpy.mockResolvedValueOnce({ rows: [], command: '', fields: null, rowCount: 0, oid: 0 }) const result = await deviceTable.insert(device) @@ -388,7 +413,15 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } querySpy.mockRejectedValueOnce(() => { throw new Error() @@ -412,7 +445,15 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } querySpy.mockRejectedValueOnce({ code: '23505' }) try { @@ -434,7 +475,15 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } querySpy.mockResolvedValueOnce({ rows: [{ device }], command: '', fields: null, rowCount: 1, oid: 0 }) getById.mockResolvedValueOnce(device) @@ -442,7 +491,7 @@ describe('device tests', () => { expect(querySpy).toBeCalledTimes(1) expect(querySpy).toBeCalledWith(` UPDATE devices - SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9 + SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9, deviceinfo=$10 WHERE guid=$1 and tenantid = $7`, [ device.guid, @@ -453,7 +502,8 @@ describe('device tests', () => { device.mpsusername, device.tenantId, device.friendlyName, - device.dnsSuffix + device.dnsSuffix, + JSON.stringify(device.deviceInfo) ]) expect(getById).toBeCalledTimes(1) expect(result).toBe(device) @@ -470,7 +520,15 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } querySpy.mockResolvedValueOnce({ rows: [], command: '', fields: null, rowCount: 0, oid: 0 }) try { @@ -492,7 +550,15 @@ describe('device tests', () => { mpsusername: 'admin', tenantId: null, friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } querySpy.mockRejectedValueOnce(() => { throw new Error() diff --git a/src/data/postgres/tables/device.ts b/src/data/postgres/tables/device.ts index 53ce22449..0d3545846 100644 --- a/src/data/postgres/tables/device.ts +++ b/src/data/postgres/tables/device.ts @@ -43,7 +43,8 @@ export class DeviceTable implements IDeviceTable { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tenantid = $3 ORDER BY guid @@ -82,7 +83,8 @@ export class DeviceTable implements IDeviceTable { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE guid = $1 and tenantid = $2` let params = [id, tenantId] @@ -96,7 +98,8 @@ export class DeviceTable implements IDeviceTable { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE guid = $1` params = [id] @@ -116,7 +119,8 @@ export class DeviceTable implements IDeviceTable { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE ${columnName} = $1 and tenantid = $2`, [queryValue, tenantId]) @@ -144,7 +148,8 @@ export class DeviceTable implements IDeviceTable { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tags @> $1 and tenantId = $4 ORDER BY guid @@ -160,7 +165,8 @@ export class DeviceTable implements IDeviceTable { mpsusername as "mpsusername", tenantid as "tenantId", friendlyname as "friendlyName", - dnssuffix as "dnsSuffix" + dnssuffix as "dnsSuffix", + deviceinfo as "deviceInfo" FROM devices WHERE tags && $1 and tenantId = $4 ORDER BY guid @@ -185,8 +191,8 @@ export class DeviceTable implements IDeviceTable { async insert (device: Device): Promise { try { const results = await this.db.query(` - INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix) - values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9)`, + INSERT INTO devices(guid, hostname, tags, mpsinstance, connectionstatus, mpsusername, tenantid, friendlyname, dnssuffix, deviceinfo) + values($1, $2, ARRAY(SELECT json_array_elements_text($3)), $4, $5, $6, $7, $8, $9, $10)`, [ device.guid, device.hostname, @@ -196,7 +202,8 @@ export class DeviceTable implements IDeviceTable { device.mpsusername, device.tenantId, device.friendlyName, - device.dnsSuffix + device.dnsSuffix, + JSON.stringify(device.deviceInfo) ]) if (results.rowCount > 0) { return await this.getById(device.guid) @@ -220,7 +227,7 @@ export class DeviceTable implements IDeviceTable { try { const results = await this.db.query(` UPDATE devices - SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9 + SET tags=$2, hostname=$3, mpsinstance=$4, connectionstatus=$5, mpsusername=$6, friendlyname=$8, dnssuffix=$9, deviceinfo=$10 WHERE guid=$1 and tenantid = $7`, [ device.guid, @@ -231,7 +238,8 @@ export class DeviceTable implements IDeviceTable { device.mpsusername, device.tenantId, device.friendlyName, - device.dnsSuffix + device.dnsSuffix, + JSON.stringify(device.deviceInfo) ]) if (results.rowCount > 0) { return await this.getById(device.guid) diff --git a/src/models/models.ts b/src/models/models.ts index 72467afb5..0b7f3ed59 100644 --- a/src/models/models.ts +++ b/src/models/models.ts @@ -18,6 +18,15 @@ export interface Device { tenantId: string friendlyName: string dnsSuffix: string + deviceInfo: DeviceInfo +} +export interface DeviceInfo { + amtVersion: string + amtBuild: string + amtSku: string + currentMode: string + ipAddress: string + dnsSuffixOs: string } export type Credentials = Record export interface AMTCredential { diff --git a/src/routes/devices/create.test.ts b/src/routes/devices/create.test.ts index 3898d06b6..2fca663f7 100644 --- a/src/routes/devices/create.test.ts +++ b/src/routes/devices/create.test.ts @@ -29,7 +29,15 @@ beforeEach(() => { tags: [], mpsusername: 'userName01', friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } reqDevice = { guid, diff --git a/src/routes/devices/get.test.ts b/src/routes/devices/get.test.ts index 1bb82d06d..a67e73b83 100644 --- a/src/routes/devices/get.test.ts +++ b/src/routes/devices/get.test.ts @@ -26,7 +26,15 @@ beforeEach(() => { connectionStatus: true, mpsusername: 'userName01', friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } req = { params: { guid }, diff --git a/src/routes/devices/getAll.test.ts b/src/routes/devices/getAll.test.ts index aaade7d4e..ee3853452 100644 --- a/src/routes/devices/getAll.test.ts +++ b/src/routes/devices/getAll.test.ts @@ -28,7 +28,15 @@ beforeEach(() => { connectionStatus: true, mpsusername: 'userName01', friendlyName: null, - dnsSuffix: null + dnsSuffix: null, + deviceInfo: { + amtVersion: '16.1', + amtBuild: '1111', + amtSku: '16392', + currentMode: '0', + ipAddress: '', + dnsSuffixOs: '' + } } allDevices = [ mockDevice,