Skip to content
Merged
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
3 changes: 3 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ EXPO_ACCESS_TOKEN=****

# Trefle plant API
TREFLE_ACCESS_TOKEN=usr-****

# OpenAI API key
OPENAI_API_KEY=****
3 changes: 2 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@plannting/api",
"version": "0.5.0",
"version": "0.6.0",
"private": true,
"main": "dist/index.js",
"scripts": {
Expand All @@ -27,6 +27,7 @@
"mongodb": "^6.19.0",
"mongoose": "^8.18.1",
"nodemailer": "^6.9.8",
"openai": "^6.16.0",
"superjson": "^1.13.3",
"zod": "^4.1.9"
},
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const config: ApiConfig = {
trefle: {
accessToken: process.env.TREFLE_ACCESS_TOKEN,
},
openai: {
apiKey: process.env.OPENAI_API_KEY,
},
}

export * from './types'
3 changes: 3 additions & 0 deletions apps/api/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ export type ApiConfig = {
trefle: {
accessToken: string | undefined,
},
openai: {
apiKey: string | undefined,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { z } from 'zod'

import { authProcedure } from '../../../procedures/authProcedure'

import { getFertilizerRecommendations as getFertilizerRecommendationsService } from '../../../services/species'

export const getFertilizerRecommendations = authProcedure
.input(z.object({
speciesId: z.string(),
}))
.query(async ({ ctx, input }) => {
// userId is always defined in authProcedure
const fertilizerRecommendations = await getFertilizerRecommendationsService({
speciesId: input.speciesId,
userId: ctx.userId!,
})

return {
text: fertilizerRecommendations.text,
recommendedUserFertilizers: fertilizerRecommendations.recommendedUserFertilizers,
recommendedProducts: fertilizerRecommendations.recommendedProducts,
}
})
13 changes: 13 additions & 0 deletions apps/api/src/models/Species.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import mongoose from 'mongoose'

export interface ISpeciesFertilizerRecommendations {
text?: string,
recommendedProducts?: string[],
}

export interface ISpecies {
_id: string,
source: 'trefle',
Expand All @@ -11,6 +16,7 @@ export interface ISpecies {
synonyms: string[] | null,
genus: string | null,
family: string | null,
fertilizerRecommendations: ISpeciesFertilizerRecommendations | null,
createdAt: Date,
updatedAt: Date,
}
Expand Down Expand Up @@ -54,6 +60,13 @@ export const speciesSchema = new mongoose.Schema<ISpecies>({
type: String,
default: null,
},
fertilizerRecommendations: {
type: {
text: String,
recommendedProducts: [String],
},
default: null,
},
}, {
collation: { locale: 'en', strength: 2 },
timestamps: true,
Expand Down
35 changes: 35 additions & 0 deletions apps/api/src/models/SpeciesUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import mongoose from 'mongoose'

export interface ISpeciesUser {
_id: string,
species: mongoose.Types.ObjectId,
user: mongoose.Types.ObjectId,
recommendedFertilizers: mongoose.Types.ObjectId[],
createdAt: Date,
updatedAt: Date,
}

export const speciesUserSchema = new mongoose.Schema<ISpeciesUser>({
species: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Species',
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
recommendedFertilizers: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'Fertilizer',
},
}, {
collation: { locale: 'en', strength: 2 },
timestamps: true,
})

// Create unique index on species + user to prevent duplicates
speciesUserSchema.index({ species: 1, user: 1 }, { unique: true })

export const SpeciesUser = mongoose.model<ISpeciesUser>('SpeciesUser', speciesUserSchema)
1 change: 1 addition & 0 deletions apps/api/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './PasswordResetToken'
export * from './Plant'
export * from './PlantLifecycleEvent'
export * from './Species'
export * from './SpeciesUser'
export * from './User'
2 changes: 2 additions & 0 deletions apps/api/src/routers/trpc/species.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { router } from '../../trpc'

import { getFertilizerRecommendations } from '../../endpoints/trpc/species/getFertilizerRecommendations'
import { listSpecies } from '../../endpoints/trpc/species/listSpecies'

export const speciesRouter = router({
getFertilizerRecommendations,
list: listSpecies,
})

Expand Down
14 changes: 14 additions & 0 deletions apps/api/src/services/openAi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import OpenAI from 'openai'

import { config } from '../../config'

export const createClient = () => {
// Initialize OpenAI client
if (!config.openai.apiKey) {
throw new Error('OpenAI API key is not configured')
}

return new OpenAI({
apiKey: config.openai.apiKey,
})
}
Loading