This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use parent id to associate child with parent as one to many relationship This way we dont have to maintain child list with parent and a parent's children can be found by simple find query Closes #39 Related #34
- Loading branch information
Showing
10 changed files
with
168 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { IIotRoom } from '../room/iot-room-model'; | ||
import { IRoom } from '../room/room-model'; | ||
|
||
export interface IDeviceGroup { | ||
_id: string; // join([IUser._id, name], '-') | ||
name: string; | ||
ownerId: string; | ||
userIds: string[]; | ||
|
||
rooms: IIotRoom[]; | ||
userIds: string[]; // IUser._id | ||
|
||
rooms?: IRoom[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { HookContext, HooksObject } from '@feathersjs/feathers'; | ||
|
||
import { logger } from '../logger'; | ||
import { Utility } from '../utility'; | ||
|
||
import { IRoom } from '../room/room-model'; | ||
import { IIotAgent } from './iot-agent-model'; | ||
|
||
export const iotAgentHooks: Partial<HooksObject> = { | ||
before: { | ||
async create(context: HookContext<IIotAgent>) { | ||
const data = context.data; | ||
const params = context.params; | ||
const { roomId }: { roomId: string } = <any>params.query; | ||
|
||
const room: IRoom = await context.app.service('rooms').get(roomId); | ||
const agentId = Utility.generateId(roomId, data.site); | ||
|
||
// #region Validation | ||
if (room === undefined) { | ||
const message = | ||
'Given device group does not exist. Try different group or create one'; | ||
logger.warn(message, { device: data, room }); | ||
throw new Error(message); | ||
} | ||
|
||
if (await Utility.isChild(agentId, context.service, { roomId })) { | ||
const message = 'Agent already exists for given room'; | ||
logger.warn(message, { device: data, room }); | ||
throw new Error(message); | ||
} | ||
// #endregion | ||
|
||
// Add reverse reference | ||
data.roomId = roomId; | ||
|
||
data._id = agentId; | ||
|
||
return context; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IIotDevice } from '../iot-device/iot-device-model'; | ||
|
||
export interface IIotAgent { | ||
_id: string; // join([IRoom._id, site], '-') | Also used for channel | ||
site: string; | ||
|
||
roomId: string; // IRoom._id | ||
|
||
devices?: IIotDevice[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export interface IIotDevice { | ||
_id: string; // join([IDeviceGroup._id, IIotRoom._id, name], '-') | ||
_id: string; // join([IIotAgent._id, name], '-') | ||
name: string; | ||
pin: number; | ||
isOn: boolean; | ||
roomId: string; | ||
groupId: string; | ||
|
||
agentId: string; // IIotAgent._id | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { HookContext, HooksObject } from '@feathersjs/feathers'; | ||
|
||
import { logger } from '../logger'; | ||
import { Utility } from '../utility'; | ||
|
||
import { IDeviceGroup } from '../device-group/device-group-model'; | ||
import { IRoom } from './room-model'; | ||
|
||
export const roomHooks: Partial<HooksObject> = { | ||
before: { | ||
async create(context: HookContext<IRoom>) { | ||
const data = context.data; | ||
const params = context.params; | ||
const { deviceGroupId }: { deviceGroupId: string } = <any>params.query; | ||
|
||
const deviceGroup: IDeviceGroup = await context.app | ||
.service('groups') | ||
.get(deviceGroupId); | ||
const roomId = Utility.generateId(deviceGroupId, data.name); | ||
|
||
//#region Validation | ||
if (deviceGroup === undefined) { | ||
const message = | ||
'Given device group does not exist. Try different group or create one'; | ||
logger.warn(message, { device: data, deviceGroupId }); | ||
throw new Error(message); | ||
} | ||
|
||
if ( | ||
await Utility.isChild(roomId, context.service, { deviceGroupId }) | ||
) { | ||
const message = 'Room already exists for given device group'; | ||
logger.warn(message, { data }); | ||
throw new Error(message); | ||
} | ||
// #endregion | ||
|
||
// Add reverse reference | ||
data.deviceGroupId = deviceGroupId; | ||
|
||
data._id = roomId; | ||
|
||
return context; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IIotAgent } from '../iot-agent/iot-agent-model'; | ||
|
||
export interface IRoom { | ||
_id: string; // join([IDeviceGroup._id, name], '-') | ||
name: string; | ||
|
||
deviceGroupId: string; // IDeviceGroup._id | ||
|
||
agents?: IIotAgent[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,34 @@ | ||
import { Service } from '@feathersjs/feathers'; | ||
import * as lodash from 'lodash'; | ||
|
||
export namespace Utility { | ||
export const equalsIgnoreCase = (string1: string, string2: string) => { | ||
return string1.toUpperCase() === string2.toUpperCase(); | ||
}; | ||
|
||
export const findIdsWhere = (service: Service<any>, parentIdKvPair) => | ||
service.find({ | ||
query: { $select: ['_id'], ...parentIdKvPair } | ||
}); | ||
|
||
export const includes = async (id: string, ids: string[]) => { | ||
return ( | ||
!(lodash.isNil(ids) || lodash.isEmpty(ids)) && lodash.includes(ids, id) | ||
); | ||
}; | ||
|
||
export const isChild = async (childId, childStore, parentIdKvPair) => { | ||
return includes(childId, await findIdsWhere(childStore, parentIdKvPair)); | ||
}; | ||
|
||
export const generateId = (parentId, param) => { | ||
const updatedId = lodash | ||
.chain(param) | ||
.map(char => lodash.toLower(char)) | ||
.filter(char => char >= 'a' && char <= 'z') | ||
.join('') | ||
.value(); | ||
|
||
return lodash.join([parentId, updatedId], '-'); | ||
}; | ||
} |