diff --git a/Applications/api/functions/src/modules/farmer/crops/CropsService.ts b/Applications/api/functions/src/modules/farmer/crops/CropsService.ts index 944e3ec1..0e8c84c9 100644 --- a/Applications/api/functions/src/modules/farmer/crops/CropsService.ts +++ b/Applications/api/functions/src/modules/farmer/crops/CropsService.ts @@ -1,14 +1,17 @@ import { db } from "../../../utils"; import { UserDetails } from "../../auth/contextDto"; +import { CommonService } from "../services/CommonServices"; 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 }); + + 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, @@ -24,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, @@ -39,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(); @@ -53,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/cropDto.ts b/Applications/api/functions/src/modules/farmer/crops/schema/cropDto.ts index 3c0c426f..422a6e0f 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 57ad3907..e290cbc0 100644 --- a/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql +++ b/Applications/api/functions/src/modules/farmer/crops/schema/crops.graphql @@ -2,21 +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 - userId: ID - type: String! stage: String! plantingDate: String! harvestDate: String - healthStatus: String! + title: String + description: String + image: String + perimeterSize: String + } type CroRresponse { success: Boolean @@ -24,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 6ee006fa..e7e07ac9 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/farm/farmService.ts b/Applications/api/functions/src/modules/farmer/farm/farmService.ts index d7ac2189..3adc7534 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 bff1720b..1b36a24f 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,16 @@ type Farm { userId: ID name: String location: String - size: Float + size: String description: String + type: FarmType + livestockIds: [String] + farmIds: [String] + cropIds: [String] } + input FarmInput { name: String location: String diff --git a/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts b/Applications/api/functions/src/modules/farmer/iotdevice/iotdeviceServices.ts index 210a7cf9..f82da8e0 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('Iotdevices').add({ + 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 8dcebc45..8bd0a847 100644 --- a/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql +++ b/Applications/api/functions/src/modules/farmer/iotdevice/schema/iotdevice.graphql @@ -1,32 +1,23 @@ type IoTDevice { - deviceId: ID! - userId: ID! - farmId: ID! - type: String! - location: Location! - data: String! - lastUpdated: String! + deviceId: ID + userId: ID + farmId: ID + deviceType: String + deviceName: String } - type Location { - latitude: Float! - longitude: Float! - } + input IoTDeviceInput { deviceId: ID! userId: ID! farmId: ID! type: String! - location: Locations! data: String! lastUpdated: String! } -input Locations { - latitude: Float! - longitude: Float! -} + type DeviceResponse { @@ -34,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 00000000..484dfaeb --- /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 7dea519a..32af8d52 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 a1f157c5..e364b081 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/modules/farmer/livestock/LivestockService.ts b/Applications/api/functions/src/modules/farmer/livestock/LivestockService.ts index 1cbb46f2..c21b91b1 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,38 @@ 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('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 c8557e83..30d21c2d 100644 --- a/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql +++ b/Applications/api/functions/src/modules/farmer/livestock/schema/livestock.graphql @@ -2,27 +2,30 @@ 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 userId: ID - type: String! - quantity: Int! - healthStatus: String! - location: String! - dateCreated: String! + description: String + quantity: String + title: String + image: String + } type LivestockRresponse { success: Boolean message: String + livestoks: [Livestock] } extend type Query { getLivestock: [Livestock] + getLivestockById(livestockId:String!): LivestockRresponse + getLivestockByfarmId(farmId: ID): [Livestock] } extend type Mutation { 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 a9559de0..f624f1cb 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 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 be46c9aa..d7736122 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,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.getLivestockByfarmId(args.farmId, 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 41fe343e..fb8cd35f 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 +} diff --git a/Applications/api/functions/src/resolvers/query.resolvers.ts b/Applications/api/functions/src/resolvers/query.resolvers.ts index 55c024c1..d90ffbcf 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