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
15 changes: 15 additions & 0 deletions apps/api/src/endpoints/trpc/chores/archiveChore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'

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

export const archiveChore = choreProcedure
.input(z.object({
id: z.string(),
}))
.mutation(async ({ ctx }) => {
ctx.chore.deletedAt = new Date()

await ctx.chore.save()

return ctx.chore
})
3 changes: 3 additions & 0 deletions apps/api/src/endpoints/trpc/chores/listChores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const defaultSortBy: { field: 'createdAt' | 'nextDate' | 'plant', direction: 'de

export const listChores = authProcedure
.input(z.object({
includeDeletedItems: z.boolean().optional().default(false),
includeDoneItems: z.boolean().optional(),
includeNotYetDueItems: z.boolean().optional(),
q: z.string().optional(),
Expand All @@ -20,12 +21,14 @@ export const listChores = authProcedure
})
.optional()
.default({
includeDeletedItems: false,
sortBy: defaultSortBy,
})
)
.query(async ({ ctx, input }) => {
const chores = await choresService.getChores({
userId: ctx.userId,
includeDeletedItems: input.includeDeletedItems,
includeDoneItems: input.includeDoneItems,
includeNotYetDueItems: input.includeNotYetDueItems,
q: input.q,
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/endpoints/trpc/chores/unarchiveChore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'

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

export const unarchiveChore = choreProcedure
.input(z.object({
id: z.string(),
}))
.mutation(async ({ ctx }) => {
ctx.chore.deletedAt = null

await ctx.chore.save()

return ctx.chore
})
15 changes: 15 additions & 0 deletions apps/api/src/endpoints/trpc/fertilizers/archiveFertilizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'

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

export const archiveFertilizer = fertilizerProcedure
.input(z.object({
id: z.string(),
}))
.mutation(async ({ ctx }) => {
ctx.fertilizer.deletedAt = new Date()

await ctx.fertilizer.save()

return ctx.fertilizer
})
6 changes: 5 additions & 1 deletion apps/api/src/endpoints/trpc/fertilizers/listFertilizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const defaultSortBy: { field: 'name', direction: 'desc' | 'asc' }[] = [ { field:

export const listFertilizers = authProcedure
.input(z.object({
includeDeletedItems: z.boolean().optional().default(false),
q: z.string().optional(),
sortBy: z.array(z.object({
field: z.enum(['name']),
Expand All @@ -21,7 +22,7 @@ export const listFertilizers = authProcedure
.default(defaultSortBy),
})
.optional()
.default({ sortBy: defaultSortBy })
.default({ includeDeletedItems: false, sortBy: defaultSortBy })
)
.query(async ({ ctx, input }) => {
const query: FilterQuery<IFertilizer> = { user: ctx.userId }
Expand All @@ -31,6 +32,9 @@ export const listFertilizers = authProcedure
{ notes: { $regex: input.q, $options: 'i' } },
]
}
if (!input.includeDeletedItems) {
query.deletedAt = null
}

const fertilizers = await Fertilizer
.find(query)
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/endpoints/trpc/fertilizers/unarchiveFertilizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'

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

export const unarchiveFertilizer = fertilizerProcedure
.input(z.object({
id: z.string(),
}))
.mutation(async ({ ctx }) => {
ctx.fertilizer.deletedAt = null

await ctx.fertilizer.save()

return ctx.fertilizer
})
15 changes: 15 additions & 0 deletions apps/api/src/endpoints/trpc/plants/archivePlant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'

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

export const archivePlant = plantProcedure
.input(z.object({
id: z.string(),
}))
.mutation(async ({ ctx }) => {
ctx.plant.deletedAt = new Date()

await ctx.plant.save()

return ctx.plant
})
6 changes: 5 additions & 1 deletion apps/api/src/endpoints/trpc/plants/listPlants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultSortBy: { field: 'name' | 'plantedAt' | 'createdAt', direction: 'de

export const listPlants = authProcedure
.input(z.object({
includeDeletedItems: z.boolean().optional().default(false),
q: z.string().optional(),
sortBy: z.array(z.object({
field: z.enum(['name', 'plantedAt', 'createdAt']),
Expand All @@ -27,7 +28,7 @@ export const listPlants = authProcedure
.default(defaultSortBy),
})
.optional()
.default({ sortBy: defaultSortBy })
.default({ includeDeletedItems: false, sortBy: defaultSortBy })
)
.query(async ({ ctx, input }) => {
const query: FilterQuery<IPlant> = { user: ctx.userId }
Expand All @@ -37,6 +38,9 @@ export const listPlants = authProcedure
{ notes: { $regex: input.q, $options: 'i' } },
]
}
if (!input.includeDeletedItems) {
query.deletedAt = null
}

const plants = await Plant
.find(query)
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/endpoints/trpc/plants/unarchivePlant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'

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

export const unarchivePlant = plantProcedure
.input(z.object({
id: z.string(),
}))
.mutation(async ({ ctx }) => {
ctx.plant.deletedAt = null

await ctx.plant.save()

return ctx.plant
})
4 changes: 4 additions & 0 deletions apps/api/src/models/Chore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface IChore {

createdAt: Date,
updatedAt: Date,
deletedAt: Date | null,
}

// export type DocIChore = mongoose.Document & Omit<IChore, '_id' | 'createdAt' | 'updatedAt'>
Expand Down Expand Up @@ -75,6 +76,9 @@ export const choreSchema = new mongoose.Schema<IChore>({
type: [mongoose.Schema.Types.ObjectId],
ref: 'ChoreLog',
},
deletedAt: {
type: Date,
},
}, {
collation: { locale: 'en', strength: 2 },
timestamps: true,
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/models/Fertilizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export const fertilizerSchema = new mongoose.Schema<IFertilizer>({
potassium: {
type: Number,
},
deletedAt: {
type: Date,
},
}, {
collation: { locale: 'en', strength: 2 },
timestamps: true,
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/models/Plant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const plantSchema = new mongoose.Schema<IPlant>({
type: String,
default: null,
},
deletedAt: {
type: Date,
},
}, {
collation: { locale: 'en', strength: 2 },
timestamps: true,
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/routers/trpc/chores.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { router } from '../../trpc'

import { archiveChore } from '../../endpoints/trpc/chores/archiveChore'
import { createChore } from '../../endpoints/trpc/chores/createChore'
import { deleteChore } from '../../endpoints/trpc/chores/deleteChore'
import { listChores } from '../../endpoints/trpc/chores/listChores'
import { unarchiveChore } from '../../endpoints/trpc/chores/unarchiveChore'
import { updateChore } from '../../endpoints/trpc/chores/updateChore'

export const choresRouter = router({
archive: archiveChore,
create: createChore,
delete: deleteChore,
list: listChores,
unarchive: unarchiveChore,
update: updateChore,
})

Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/routers/trpc/fertilizers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { router } from '../../trpc'

import { archiveFertilizer } from '../../endpoints/trpc/fertilizers/archiveFertilizer'
import { createFertilizer } from '../../endpoints/trpc/fertilizers/createFertilizer'
import { deleteFertilizer } from '../../endpoints/trpc/fertilizers/deleteFertilizer'
import { listFertilizers } from '../../endpoints/trpc/fertilizers/listFertilizers'
import { unarchiveFertilizer } from '../../endpoints/trpc/fertilizers/unarchiveFertilizer'
import { updateFertilizer } from '../../endpoints/trpc/fertilizers/updateFertilizer'

export const fertilizersRouter = router({
archive: archiveFertilizer,
create: createFertilizer,
delete: deleteFertilizer,
list: listFertilizers,
unarchive: unarchiveFertilizer,
update: updateFertilizer,
})

Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/routers/trpc/plants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { router } from '../../trpc'

import { archivePlant } from '../../endpoints/trpc/plants/archivePlant'
import { createPlant } from '../../endpoints/trpc/plants/createPlant'
import { deletePlant } from '../../endpoints/trpc/plants/deletePlant'
import { listPlantLifecycleEvents } from '../../endpoints/trpc/plants/listPlantLifecycleEvents'
import { listPlants } from '../../endpoints/trpc/plants/listPlants'
import { unarchivePlant } from '../../endpoints/trpc/plants/unarchivePlant'
import { updatePlant } from '../../endpoints/trpc/plants/updatePlant'

export const plantsRouter = router({
archive: archivePlant,
create: createPlant,
delete: deletePlant,
list: listPlants,
listLifecycleEvents: listPlantLifecycleEvents,
unarchive: unarchivePlant,
update: updatePlant,
})

Expand Down
12 changes: 12 additions & 0 deletions apps/api/src/services/chores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ import {
} from '../../models'

export const getChores = async ({
includeDeletedItems = false,
includeDoneItems = true,
includeNotYetDueItems = true,
q,
sortBy,
userId,
}: {
includeDeletedItems?: boolean,
includeDoneItems?: boolean,
includeNotYetDueItems?: boolean,
q?: string,
sortBy?: { field: 'nextDate' | 'createdAt' | 'plant', direction: 'asc' | 'desc' }[],
userId?: string,
}) => {
const query: FilterQuery<IChore> = userId ? { user: userId } : {}
if (!includeDeletedItems) {
query.deletedAt = null
}

if (q?.trim()) {
const searchRegex = { $regex: q?.trim(), $options: 'i' }
Expand All @@ -39,6 +44,7 @@ export const getChores = async ({
await Plant
.find({
...(userId ? { user: userId } : {}),
...(!includeDeletedItems ? { deletedAt: null } : {}),
$or: [
{ name: searchRegex },
{ notes: searchRegex },
Expand All @@ -60,6 +66,7 @@ export const getChores = async ({
await Fertilizer
.find({
...(userId ? { user: userId } : {}),
...(!includeDeletedItems ? { deletedAt: null } : {}),
$or: [
{ name: searchRegex },
{ notes: searchRegex },
Expand Down Expand Up @@ -115,6 +122,11 @@ export const getChores = async ({
chores = chores.filter((chore) => !chore.isDone)
}

// Filter out chores for archived plants if includeDeletedItems is false
if (includeDeletedItems === false) {
chores = chores.filter((chore) => !chore.plant?.deletedAt)
}

// Sort by all criteria at once
// Each sort after the first is only applied if the previous sort was a tie
if (sortBy && sortBy.length > 0) {
Expand Down
Loading