From 7c4e661c117a71ccd81983aa5a85f995887ae170 Mon Sep 17 00:00:00 2001 From: Daniel Mokoena Date: Mon, 22 May 2023 12:21:17 +0200 Subject: [PATCH 1/6] feat(api): fix collection mapping --- .../src/modules/farmer/crops/CropsService.ts | 3 + .../modules/farmer/crops/schema/crops.graphql | 1 - .../src/modules/farmer/farm/farmService.ts | 4 +- .../modules/farmer/farm/schema/farm.graphql | 6 +- .../farmer/livestock/LivestockService.ts | 32 +++-- .../farmer/livestock/schema/livestock.graphql | 3 +- .../livestock/schema/query.resolvers.ts | 5 + .../modules/farmer/services/CommonServices.ts | 121 ++++-------------- 8 files changed, 64 insertions(+), 111 deletions(-) diff --git a/Applications/api/functions/src/modules/farmer/crops/CropsService.ts b/Applications/api/functions/src/modules/farmer/crops/CropsService.ts index 944e3ec..390cd8b 100644 --- a/Applications/api/functions/src/modules/farmer/crops/CropsService.ts +++ b/Applications/api/functions/src/modules/farmer/crops/CropsService.ts @@ -1,5 +1,6 @@ import { db } from "../../../utils"; import { UserDetails } from "../../auth/contextDto"; +import { CommonService } from "../services/CommonServices"; import { CropInput } from "./schema/cropDto"; export class CropsService { @@ -9,6 +10,8 @@ export class CropsService { ...crop, userId: user.uid }); + + await CommonService.generalCollectionMapper(cropRef.id, crop.farmId, 'Farm', "CropIds") // Return the newly created crop with its ID return { message: "Crop details successfully added", success: true, diff --git a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql index 57ad390..2fbcf1c 100644 --- a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql +++ b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql @@ -11,7 +11,6 @@ type Crop { } input CropInput { farmId: ID - userId: ID type: String! stage: String! plantingDate: String! diff --git a/Applications/api/functions/src/modules/farmer/farm/farmService.ts b/Applications/api/functions/src/modules/farmer/farm/farmService.ts index d7ac218..3adc753 100644 --- a/Applications/api/functions/src/modules/farmer/farm/farmService.ts +++ b/Applications/api/functions/src/modules/farmer/farm/farmService.ts @@ -1,5 +1,6 @@ import { db } from "../../../utils"; import { UserDetails } from "../../auth/contextDto"; +import { CommonService } from "../services/CommonServices"; import { FarmInput } from "./schema/farmDto"; @@ -11,6 +12,7 @@ export class FarmService { ...farm, userId: user.uid }); + await CommonService.generalCollectionMapper(farmRef.id,user.uid,"User","farmIds") // Return the newly created farm with its ID return { message: "Farm details successfully added", success: true, @@ -48,7 +50,7 @@ export class FarmService { const farms: any[] = []; snapshot.forEach(doc => { - farms.push({ id: doc.id, ...doc.data() }); + farms.push({ farmId: doc.id, ...doc.data() }); }); console.log(farms); diff --git a/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql b/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql index bff1720..fd863b4 100644 --- a/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql +++ b/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql @@ -3,11 +3,15 @@ type Farm { userId: ID name: String location: String - size: Float + size: String description: String + type: FarmType + livestockIds: [String] + farmIds: [String] } + input FarmInput { name: String location: String diff --git a/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts b/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts index 1cbb46f..330a102 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts +++ b/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts @@ -1,13 +1,26 @@ import { db } from "../../../utils"; import { UserDetails } from "../../auth/contextDto"; +import { CommonService } from "../services/CommonServices"; import { LivestockInput } from "./schema/livestockDto"; export class LivestockService { - static async addStock(stock: any,user:UserDetails) { - const livestockRef = await db.collection('Livestocks').add({ + static async getLivestockId(livestockId: string, user: UserDetails) { + try { + const livestocks = (await db.collection("Livestock").doc(livestockId).get()).data(); + return { livestocks, message: `livestock data fetched for ${user.displayName}`, success: true } + } catch (error) { + return { + message: '', success: false + } + } + } + static async addStock(stock: any, user: UserDetails) { + const livestockRef = await db.collection('Livestock').add({ ...stock, userId: user.uid }); + + await CommonService.generalCollectionMapper(livestockRef.id, stock.farmId, "Farm", "livestockIds") // Return the newly created livestock with its ID return { message: "Livestock details successfully added", success: true, @@ -22,9 +35,8 @@ export class LivestockService { static async updateLivestock(livestockId: string, updates: LivestockInput) { try { - // Update the farm document with the specified ID - await db.collection('Livestocks').doc(livestockId).update({...updates}); + await db.collection('Livestocks').doc(livestockId).update({ ...updates }); // Return a success message return { message: "Livestocks details successfully updated", success: true, @@ -36,22 +48,22 @@ export class LivestockService { }; } } - + static async getLivestock(user: UserDetails): Promise { - const livestockRef = db.collection('Livestocks'); + const livestockRef = db.collection('Livestock'); const query = livestockRef.where('userId', '==', user.uid); - + const snapshot = await query.get(); const livestocks: any[] = []; - + snapshot.forEach(doc => { livestocks.push({ id: doc.id, ...doc.data() }); }); console.log(livestocks); - + return livestocks; -} + } diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql index c8557e8..224d4b7 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql @@ -10,7 +10,6 @@ type Livestock { } input LivestockInput { farmId: ID - userId: ID type: String! quantity: Int! healthStatus: String! @@ -20,9 +19,11 @@ input LivestockInput { type LivestockRresponse { success: Boolean message: String + livestoks: [Livestock] } extend type Query { getLivestock: [Livestock] + getLivestockById(livestockId:String!): LivestockRresponse } extend type Mutation { diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts b/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts index be46c9a..a7cb651 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts @@ -5,4 +5,9 @@ export const LivestockQueryResolver = { getLivestock: async (_parent: unknown, __: any, context: { user: UserDetails }) => { return LivestockService.getLivestock(context.user); }, + + getLivestockById: async(_:unknown, args: {livestockId: string}, context: {user: UserDetails})=>{ + return LivestockService.getLivestockId(args.livestockId, context.user) + + } } \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/services/CommonServices.ts b/Applications/api/functions/src/modules/farmer/services/CommonServices.ts index 41fe343..fb8cd35 100644 --- a/Applications/api/functions/src/modules/farmer/services/CommonServices.ts +++ b/Applications/api/functions/src/modules/farmer/services/CommonServices.ts @@ -1,105 +1,32 @@ // /* eslint-disable valid-jsdoc */ // /* eslint-disable @typescript-eslint/ban-ts-comment */ -// import { firestore } from "firebase-admin"; -// import { db } from "../../../utils"; +import { firestore } from "firebase-admin"; +import { db } from "../../../utils"; // ///import { ICollectionTypes } from "./UserDto"; // /** // * Common service // */ -// export class CommonService { -// /** -// * Maps two colections based on content ids and field name -// * @param {string} childId - The child collecion id -// * @param {string} parentId - The parent collecion id -// * @param {string} collection - Collection name -// * @param {string} field - Collection name -// */ -// static async generalCollectionMapper(childId: string, parentId: string, collection: string, field: string) { -// try { -// const parent = db.collection(collection).doc(parentId); -// (await parent.get()).exists ? await parent.update({ -// [field]: firestore.FieldValue.arrayUnion(childId), -// }) : await parent.set({ -// [field]: [childId], -// }); -// return true; -// } catch (e) { -// return false; -// } -// } +export class CommonService { + /** + * Maps two colections based on content ids and field name + * @param {string} childId - The child collecion id + * @param {string} parentId - The parent collecion id + * @param {string} collection - Collection name + * @param {string} field - Collection field + */ + static async generalCollectionMapper(childId: string, parentId: string, collection: string, field: string) { + try { + const parent = db.collection(collection).doc(parentId); + (await parent.get()).exists ? await parent.update({ + [field]: firestore.FieldValue.arrayUnion(childId), + }) : await parent.set({ + [field]: [childId], + }); + return true; + } catch (e) { + return false; + } + } -// static async mapCollections(parentId: string, -// childId: string, collection?: ICollectionTypes): -// Promise { -// try { -// const userref = db.collection("Users").doc(childId as string); -// switch (collection) { -// case "Farm": -// (await userref.get()).exists ? -// await userref.update({ -// farmId: firestore.FieldValue.arrayUnion(parentId), -// }) : -// await userref.set({ farmId: [parentId] }); -// break; -// case "Crop": -// (await userref.get()).exists ? -// await userref.update({ cropId: firestore.FieldValue.arrayUnion(parentId) }) : -// await userref.set({ cropId: [parentId] }); - -// break; -// case "Livestock": -// (await userref.get()).exists ? -// await userref.update({ -// livestockId: firestore.FieldValue.arrayUnion(parentId), -// }) : -// await userref.set({ livestockId: [parentId] }); -// break; -// case "Weather": -// (await userref.get()).exists ? -// await userref.update({ -// experienceId: -// firestore.FieldValue.arrayUnion(parentId), -// }) : -// await userref.set({ weatherId: [parentId] }); -// break; -// case "Iotdevice": -// (await userref.get()).exists ? -// await userref.update({ -// iotdeviceId: firestore.FieldValue.arrayUnion(parentId), -// }) : -// await userref.set({ iotdeviceId: [parentId] }); -// break; -// default: -// break; -// } // if it doesn't exist, create the document -// return true; -// } catch (error) { -// return false; -// } -// } - -// /** -// * Delete collection record based on content ids and field name -// * @param {string} targetId - collection id -// * @param {string} collection - Collection name - -// */ - -// static async delete(collecion: string, targetId: string ) { -// try { -// await db.collection(collecion).doc(targetId).delete(); - -// return { -// message: "Record deleted successfully", -// success: true, -// }; -// } catch (error) { -// return { -// message: "Record could not be deleted", -// success: false, -// }; -// } -// } - -// } \ No newline at end of file +} From 3946c10de8793748e8ced24eea634bfb6637ac82 Mon Sep 17 00:00:00 2001 From: Daniel Mokoena Date: Mon, 22 May 2023 13:57:46 +0200 Subject: [PATCH 2/6] fix: vectifying schema's Co-authored-by: Mitch Chimwemwe Chanza Co-authored-by: Dineo Mathibela --- .../modules/farmer/crops/schema/cropDto.ts | 6 ++++-- .../modules/farmer/crops/schema/crops.graphql | 15 ++++++++----- .../modules/farmer/farm/schema/farm.graphql | 1 + .../farmer/livestock/schema/livestock.graphql | 21 ++++++++++--------- .../farmer/livestock/schema/livestockDto.ts | 8 ++----- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Applications/api/functions/src/modules/farmer/crops/schema/cropDto.ts b/Applications/api/functions/src/modules/farmer/crops/schema/cropDto.ts index 3c0c426..422a6e0 100644 --- a/Applications/api/functions/src/modules/farmer/crops/schema/cropDto.ts +++ b/Applications/api/functions/src/modules/farmer/crops/schema/cropDto.ts @@ -1,9 +1,11 @@ export declare interface CropInput { farmId: string userId: string - type: string + title: string + description: String + image: String stage: string plantingDate: string harvestDate: string - healthStatus: string + perimeterSize: string } \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql index 2fbcf1c..23667f1 100644 --- a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql +++ b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql @@ -2,20 +2,25 @@ type Crop { cropId: ID farmId: ID userId: ID - type: String stage: String plantingDate: String harvestDate: String - healthStatus: String - dateCreated: String + title: String + description: String + image: String + perimeterSize: String + } input CropInput { farmId: ID - type: String! stage: String! plantingDate: String! harvestDate: String - healthStatus: String! + title: String + description: String + image: String + perimeterSize: String + } type CroRresponse { success: Boolean diff --git a/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql b/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql index fd863b4..1b36a24 100644 --- a/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql +++ b/Applications/api/functions/src/modules/farmer/farm/schema/farm.graphql @@ -8,6 +8,7 @@ type Farm { type: FarmType livestockIds: [String] farmIds: [String] + cropIds: [String] } diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql index 224d4b7..c21962f 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql @@ -2,19 +2,20 @@ type Livestock { livestockId: ID farmId: ID userId: ID - type: String - quantity: Int - healthStatus: String - location: String - dateCreated: String + title: String + description: String + image: String + quantity: String + } input LivestockInput { farmId: ID - type: String! - quantity: Int! - healthStatus: String! - location: String! - dateCreated: String! + userId: ID + description: String + quantity: String + title: String + image: String + } type LivestockRresponse { success: Boolean diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/livestockDto.ts b/Applications/api/functions/src/modules/farmer/livestock/schema/livestockDto.ts index a9559de..f624f1c 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/livestockDto.ts +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/livestockDto.ts @@ -1,10 +1,6 @@ export declare interface LivestockInput { - name: any + title: any farmId: string userId: string - type: string - quantity: number - healthStatus: string - location: string - dateCreated: string + quantity: string } \ No newline at end of file From 706ed06990771c7430f68e3893be363ebf417cd7 Mon Sep 17 00:00:00 2001 From: Daniel Mokoena Date: Mon, 22 May 2023 14:41:49 +0200 Subject: [PATCH 3/6] fix: get livestock by farmId --- .../modules/farmer/livestock/LivestockService.ts | 16 ++++++++++++++++ .../farmer/livestock/schema/livestock.graphql | 1 + .../farmer/livestock/schema/query.resolvers.ts | 6 +++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts b/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts index 330a102..c21b91b 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts +++ b/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts @@ -49,6 +49,22 @@ export class LivestockService { } } + static async getLivestockByfarmId(farmId: string,user: UserDetails): Promise { + + const livestockRef = db.collection('Livestock'); + const query = livestockRef.where('farmId', '==', farmId); + + const snapshot = await query.get(); + const livestocks: any[] = []; + + snapshot.forEach(doc => { + livestocks.push({ id: doc.id, ...doc.data() }); + }); + console.log(livestocks); + + return livestocks; + } + static async getLivestock(user: UserDetails): Promise { const livestockRef = db.collection('Livestock'); diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql index c21962f..52e1b2d 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql @@ -25,6 +25,7 @@ type LivestockRresponse { extend type Query { getLivestock: [Livestock] getLivestockById(livestockId:String!): LivestockRresponse + getLivestockByfarmId(farmId: ID): LivestockRresponse } extend type Mutation { diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts b/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts index a7cb651..691f842 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts @@ -5,9 +5,13 @@ export const LivestockQueryResolver = { getLivestock: async (_parent: unknown, __: any, context: { user: UserDetails }) => { return LivestockService.getLivestock(context.user); }, - getLivestockById: async(_:unknown, args: {livestockId: string}, context: {user: UserDetails})=>{ return LivestockService.getLivestockId(args.livestockId, context.user) + }, + getLivestockByfarmId: async(_:unknown, args: {farmId: string}, context: {user: UserDetails})=>{ + return LivestockService.getLivestockId(args.farmId, context.user) + } + } \ No newline at end of file From 2208f9f11610e0678ff72cd20da195ffbb8c3773 Mon Sep 17 00:00:00 2001 From: Daniel Mokoena Date: Mon, 22 May 2023 14:42:23 +0200 Subject: [PATCH 4/6] fix: livestock --- .../src/modules/farmer/livestock/schema/livestock.graphql | 2 +- .../src/modules/farmer/livestock/schema/query.resolvers.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql index 52e1b2d..30d21c2 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql @@ -25,7 +25,7 @@ type LivestockRresponse { extend type Query { getLivestock: [Livestock] getLivestockById(livestockId:String!): LivestockRresponse - getLivestockByfarmId(farmId: ID): LivestockRresponse + getLivestockByfarmId(farmId: ID): [Livestock] } extend type Mutation { diff --git a/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts b/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts index 691f842..d773612 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/query.resolvers.ts @@ -10,7 +10,7 @@ export const LivestockQueryResolver = { }, getLivestockByfarmId: async(_:unknown, args: {farmId: string}, context: {user: UserDetails})=>{ - return LivestockService.getLivestockId(args.farmId, context.user) + return LivestockService.getLivestockByfarmId(args.farmId, context.user) } From e4c55921b09c01d3d913ab1eecb94a05edd9c7f9 Mon Sep 17 00:00:00 2001 From: Daniel Mokoena Date: Mon, 22 May 2023 16:42:43 +0200 Subject: [PATCH 5/6] fix: crop query by farmId --- .../src/modules/farmer/crops/CropsService.ts | 22 ++++++++++++++++--- .../modules/farmer/crops/schema/crops.graphql | 1 + .../farmer/crops/schema/query.resolvers.ts | 5 +++++ .../farmer/iotdevice/iotdeviceServices.ts | 2 +- .../farmer/iotdevice/schema/iotdevice.graphql | 19 +++++++--------- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Applications/api/functions/src/modules/farmer/crops/CropsService.ts b/Applications/api/functions/src/modules/farmer/crops/CropsService.ts index 390cd8b..0e8c84c 100644 --- a/Applications/api/functions/src/modules/farmer/crops/CropsService.ts +++ b/Applications/api/functions/src/modules/farmer/crops/CropsService.ts @@ -6,7 +6,7 @@ import { CropInput } from "./schema/cropDto"; export class CropsService { static async addCrop(crop: any, user:UserDetails) { - const cropRef = await db.collection('Crops').add({ + const cropRef = await db.collection('Crop').add({ ...crop, userId: user.uid }); @@ -27,7 +27,7 @@ export class CropsService { try { // Update the farm document with the specified ID - await db.collection('Crops').doc(cropId).update({...updates}); + await db.collection('Crop').doc(cropId).update({...updates}); // Return a success message return { message: "Crop details successfully updated", success: true, @@ -42,7 +42,7 @@ export class CropsService { static async getCrop(user: UserDetails): Promise { - const cropsRef = db.collection('Crops'); + const cropsRef = db.collection('Crop'); const query = cropsRef.where('userId', '==', user.uid); const snapshot = await query.get(); @@ -56,4 +56,20 @@ export class CropsService { return crops; } + static async getCropByfarmId(farmId: string,user: UserDetails): Promise { + + const cropRef = db.collection('Crop'); + const query = cropRef.where('farmId', '==', farmId); + + const snapshot = await query.get(); + const crops: any[] = []; + + snapshot.forEach(doc => { + crops.push({ id: doc.id, ...doc.data() }); + }); + console.log(crops); + + return crops; + } + } \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql index 23667f1..e290cbc 100644 --- a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql +++ b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql @@ -28,6 +28,7 @@ type CroRresponse { } extend type Query { getCrop: [Crop] + getCropByfarmId(farmId: ID): [Crop] } extend type Mutation { diff --git a/Applications/api/functions/src/modules/farmer/crops/schema/query.resolvers.ts b/Applications/api/functions/src/modules/farmer/crops/schema/query.resolvers.ts index 6ee006f..e7e07ac 100644 --- a/Applications/api/functions/src/modules/farmer/crops/schema/query.resolvers.ts +++ b/Applications/api/functions/src/modules/farmer/crops/schema/query.resolvers.ts @@ -1,3 +1,4 @@ +import { UserDetails } from "../../../auth/contextDto"; import { CropsService } from "../CropsService"; export const CropsQueryResolver = { @@ -5,4 +6,8 @@ export const CropsQueryResolver = { return CropsService.getCrop(context.user); }, + getCropByfarmId: async(_:unknown, args: {farmId: string}, context: {user: UserDetails})=>{ + return CropsService.getCropByfarmId(args.farmId, context.user) + + } } \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts b/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts index 210a7cf..3f0600b 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts +++ b/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts @@ -3,7 +3,7 @@ import { db } from "../../../utils/firbase.config"; export class IotdeviceService { static async addIotdevice(device: any) { // Create a new document in the "farms" collection with the specified user ID and farm details - const deviceRef = await db.collection('Iotdevices').add({ + const deviceRef = await db.collection('Iotdevice').add({ ...device, userId: "1d1d12345" //context.uid }); diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql index 8dcebc4..30b1133 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql +++ b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql @@ -1,17 +1,14 @@ type IoTDevice { - deviceId: ID! - userId: ID! - farmId: ID! - type: String! - location: Location! - data: String! - lastUpdated: String! + deviceId: ID + userId: ID + farmId: ID + type: String + location: String + data: String + lastUpdated: String } - type Location { - latitude: Float! - longitude: Float! - } + input IoTDeviceInput { deviceId: ID! From e4de03a08bda8a55865e296b1b0f9d1ddea505c0 Mon Sep 17 00:00:00 2001 From: Daniel Mokoena Date: Mon, 22 May 2023 17:55:07 +0200 Subject: [PATCH 6/6] fixL iotdevice schema rectified --- .../farmer/iotdevice/iotdeviceServices.ts | 29 +++++++++++++++---- .../farmer/iotdevice/schema/iotdevice.graphql | 15 ++++------ .../farmer/iotdevice/schema/iotdeviceDto.ts | 7 +++++ .../iotdevice/schema/mutation.resolvers.ts | 27 +++++++++-------- .../iotdevice/schema/query.resolvers.ts | 12 ++++++-- .../src/resolvers/query.resolvers.ts | 4 +-- 6 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdeviceDto.ts diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts b/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts index 3f0600b..f82da8e 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts +++ b/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts @@ -1,12 +1,15 @@ import { db } from "../../../utils/firbase.config"; +import { UserDetails } from "../../auth/contextDto"; +import { CommonService } from "../services/CommonServices"; export class IotdeviceService { - static async addIotdevice(device: any) { + static async addIotdevice(device: any, user: UserDetails) { // Create a new document in the "farms" collection with the specified user ID and farm details const deviceRef = await db.collection('Iotdevice').add({ ...device, - userId: "1d1d12345" //context.uid + userId: user.uid }); + await CommonService.generalCollectionMapper(deviceRef.id, device.farmId, 'Farm', "IotdeviceIds") // Return the newly created farm with its ID return { message: "Iotdevice details successfully added", success: true, @@ -18,10 +21,10 @@ export class IotdeviceService { }; } - static async getIotdeviceByUserId(userId: string): Promise { + static async getIotdevice(user: UserDetails): Promise { - const farmsRef = db.collection('Farms'); - const query = farmsRef.where('userId', '==', userId); + const farmsRef = db.collection('Iotdevice'); + const query = farmsRef.where('userId', '==', user.uid); const snapshot = await query.get(); const farms: any[] = []; @@ -33,4 +36,20 @@ export class IotdeviceService { return farms; } + + static async getDeviceByfarmId(farmId: string,user: UserDetails): Promise { + + const deviceRef = db.collection('Iotdevice'); + const query = deviceRef.where('farmId', '==', farmId); + + const snapshot = await query.get(); + const devices: any[] = []; + + snapshot.forEach(doc => { + devices.push({ id: doc.id, ...doc.data() }); + }); + console.log(devices); + + return devices; + } } \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql index 30b1133..8bd0a84 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql +++ b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql @@ -2,10 +2,8 @@ type IoTDevice { deviceId: ID userId: ID farmId: ID - type: String - location: String - data: String - lastUpdated: String + deviceType: String + deviceName: String } @@ -15,15 +13,11 @@ input IoTDeviceInput { userId: ID! farmId: ID! type: String! - location: Locations! data: String! lastUpdated: String! } -input Locations { - latitude: Float! - longitude: Float! -} + type DeviceResponse { @@ -31,7 +25,8 @@ type DeviceResponse { message: String } extend type Query { - getIotdeviceByUserId(userId: ID!): [IoTDevice] + getIotdevice: [IoTDevice] + getDeviceByfarmId(farmId: ID): [IoTDevice] } extend type Mutation { diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdeviceDto.ts b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdeviceDto.ts new file mode 100644 index 0000000..484dfae --- /dev/null +++ b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdeviceDto.ts @@ -0,0 +1,7 @@ +export declare interface IoTDeviceInput { + deviceId: string + userId: string + farmId: string + deviceType: string + deviceName: string +} \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/schema/mutation.resolvers.ts b/Applications/api/functions/src/modules/farmer/iotdevice/schema/mutation.resolvers.ts index 7dea519..32af8d5 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/schema/mutation.resolvers.ts +++ b/Applications/api/functions/src/modules/farmer/iotdevice/schema/mutation.resolvers.ts @@ -1,19 +1,22 @@ +import { db } from "../../../../utils/firbase.config"; +import { UserDetails } from "../../../auth/contextDto"; +import { IotdeviceService } from "../iotdeviceServices"; export const IotdeviceMutationResolver = { - // createIotdevice: (_:any, arg: {device: any}) => { - - // return IotdeviceService.addIotdevice(arg.device); - // }, - - // deleteDevice: async (_parent: unknown, args: - // { deviceId: string; }, context: { user: any }) => { + createIotdevice: (_: any, arg: { device: any }, context: {user:UserDetails}) => { + console.log(context) + return IotdeviceService.addIotdevice(arg.device, context.user); + }, + + deleteDevice: async (_parent: unknown, args: + { deviceId: string; }, context: { user: any }) => { - // await db.collection('Iotdevices').doc(args.deviceId).delete(); - // return { - // success: true, message: "successfully deleted" - // } - // }, + await db.collection('Iotdevices').doc(args.deviceId).delete(); + return { + success: true, message: "successfully deleted" + } + }, } \ No newline at end of file diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/schema/query.resolvers.ts b/Applications/api/functions/src/modules/farmer/iotdevice/schema/query.resolvers.ts index a1f157c..e364b08 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/schema/query.resolvers.ts +++ b/Applications/api/functions/src/modules/farmer/iotdevice/schema/query.resolvers.ts @@ -1,11 +1,17 @@ +import { UserDetails } from "../../../auth/contextDto"; import { IotdeviceService } from "../iotdeviceServices"; export const IotdeviceQueryResolver = { - getIotdeviceByUserId: async (_parent: unknown, args: - { userId: string; }, context: { user: any }) => { + getIotdevice: async (_parent: unknown, __: any, context: { user: UserDetails }) => { - return IotdeviceService.getIotdeviceByUserId(args.userId); + return IotdeviceService.getIotdevice(context.user); }, + getDeviceByfarmId: async(_:unknown, args: {farmId: string}, context: {user: UserDetails})=>{ + + return IotdeviceService.getDeviceByfarmId(args.farmId, context.user) + + } + } \ No newline at end of file diff --git a/Applications/api/functions/src/resolvers/query.resolvers.ts b/Applications/api/functions/src/resolvers/query.resolvers.ts index 55c024c..d90ffbc 100644 --- a/Applications/api/functions/src/resolvers/query.resolvers.ts +++ b/Applications/api/functions/src/resolvers/query.resolvers.ts @@ -2,7 +2,7 @@ import { CropsQueryResolver, LivestockQueryResolver } from "../modules"; import { FarmQueryResolver } from "../modules/farmer/farm"; -import { IotdeviceMutationResolver } from "../modules/farmer/iotdevice"; +import { IotdeviceQueryResolver } from "../modules/farmer/iotdevice"; import { UserQueryResolver } from "../modules/farmer/profile"; import { WeatherQueryResolver } from "../modules/farmer/weather"; @@ -13,7 +13,7 @@ export const QueryResolvers = { ...WeatherQueryResolver, ...FarmQueryResolver, ...LivestockQueryResolver, - ...IotdeviceMutationResolver, + ...IotdeviceQueryResolver, ...UserQueryResolver, ...CropsQueryResolver, ...mainResolver