Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -39,7 +42,7 @@ export class CropsService {

static async getCrop(user: UserDetails): Promise<any> {

const cropsRef = db.collection('Crops');
const cropsRef = db.collection('Crop');
const query = cropsRef.where('userId', '==', user.uid);

const snapshot = await query.get();
Expand All @@ -53,4 +56,20 @@ export class CropsService {
return crops;
}

static async getCropByfarmId(farmId: string,user: UserDetails): Promise<any> {

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;
}

}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ 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
message: String
}
extend type Query {
getCrop: [Crop]
getCropByfarmId(farmId: ID): [Crop]
}

extend type Mutation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { UserDetails } from "../../../auth/contextDto";
import { CropsService } from "../CropsService";

export const CropsQueryResolver = {
getCrop: async (_parent: unknown, __:any, context: { user: any }) => {

return CropsService.getCrop(context.user);
},
getCropByfarmId: async(_:unknown, args: {farmId: string}, context: {user: UserDetails})=>{
return CropsService.getCropByfarmId(args.farmId, context.user)

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db } from "../../../utils";
import { UserDetails } from "../../auth/contextDto";
import { CommonService } from "../services/CommonServices";
import { FarmInput } from "./schema/farmDto";


Expand All @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,10 +21,10 @@ export class IotdeviceService {
};
}

static async getIotdeviceByUserId(userId: string): Promise<any> {
static async getIotdevice(user: UserDetails): Promise<any> {

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[] = [];
Expand All @@ -33,4 +36,20 @@ export class IotdeviceService {

return farms;
}

static async getDeviceByfarmId(farmId: string,user: UserDetails): Promise<any> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
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 {
success: Boolean
message: String
}
extend type Query {
getIotdeviceByUserId(userId: ID!): [IoTDevice]
getIotdevice: [IoTDevice]
getDeviceByfarmId(farmId: ID): [IoTDevice]
}

extend type Mutation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export declare interface IoTDeviceInput {
deviceId: string
userId: string
farmId: string
deviceType: string
deviceName: string
}
Original file line number Diff line number Diff line change
@@ -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"
}
},

}
Original file line number Diff line number Diff line change
@@ -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)

}



}
Loading