From 74cd16e75e5a3f7c31ab6dc7e1ffe55fa5245d28 Mon Sep 17 00:00:00 2001 From: Tadjaur Date: Sun, 15 Dec 2024 11:00:43 +0100 Subject: [PATCH 1/4] Update Trip endpoints --- .../tripRoutesValidator.ts | 37 +++++++++----- server/src/controllers/trip/addTrip.ts | 14 ++--- server/src/controllers/trip/editTrip.ts | 11 ++-- server/src/controllers/trip/index.ts | 1 + server/src/db/schema.ts | 4 +- server/src/drizzle/methods/Geojson.ts | 26 ++++++++-- server/src/drizzle/methods/trip.ts | 51 ++++++++++--------- server/src/routes/trpcRouter.ts | 3 ++ server/src/services/trip/addTripService.ts | 36 +++++++++---- server/src/services/trip/trip.service.ts | 1 + 10 files changed, 116 insertions(+), 68 deletions(-) diff --git a/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts b/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts index 27afb355a..960554e0e 100644 --- a/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts +++ b/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts @@ -5,14 +5,13 @@ const tripActivityValues = Object.values(TripActivity) as [string, ...string[]]; export const addTripForm = z.object({ name: z.string(), - description: z.string(), - activity: z.enum(tripActivityValues), + description: z.string().optional().nullable(), + activity: z.enum(tripActivityValues).optional(), is_public: z.union([z.literal('0'), z.literal('1')]), }); -// @ts-ignore const coordinateSchema = z.lazy(() => - z.union([z.number(), z.array(coordinateSchema)]), + z.union([z.number(), z.array(z.number())]), ); const baseGeometrySchema = z.object({ @@ -50,31 +49,41 @@ export const getTripById = z.object({ export const addTripDetails = z.object({ start_date: z.string(), end_date: z.string(), - destination: z.string(), - activity: z.enum(tripActivityValues), + activity: z.enum(tripActivityValues).optional(), parks: z.string().optional(), trails: z.string().optional(), + packId: z.string(), geoJSON: z.string(), - owner_id: z.string(), - pack_id: z.string(), - bounds: z.tuple([z.array(z.number()), z.array(z.number())]), }); -export const addTrip = addTripDetails.merge(addTripForm); +export const addTrip = addTripDetails.merge(addTripForm).omit({ + is_public: true, +}); +export type AddTripType = z.infer; export const editTrip = z.object({ - id: z.string(), + id: z.string().min(1), name: z.string().optional(), description: z.string().optional(), - activity: z.enum(tripActivityValues).optional(), start_date: z.string().optional(), end_date: z.string().optional(), - destination: z.string().optional(), + packId: z.string().optional(), + parks: z.string().optional(), + trails: z.string().optional(), + activity: z.enum(tripActivityValues).optional(), + geoJSON: z.string().optional(), +}); + +export type EditTripType = z.infer; + +export const setTripVisibility = z.object({ + tripId: z.string().min(1), is_public: z.union([z.literal('0'), z.literal('1')]), }); +export type SetTripVisibilityType = z.infer; export const deleteTrip = z.object({ - tripId: z.string().nonempty(), + tripId: z.string().min(1), }); export const queryTrip = z.object({ diff --git a/server/src/controllers/trip/addTrip.ts b/server/src/controllers/trip/addTrip.ts index feba2d7db..d0d3d2135 100644 --- a/server/src/controllers/trip/addTrip.ts +++ b/server/src/controllers/trip/addTrip.ts @@ -1,11 +1,13 @@ -import { publicProcedure, protectedProcedure } from '../../trpc'; -import { addTripService } from '../../services/trip/addTripService'; +import { protectedProcedure } from '../../trpc'; +import { Context } from 'hono'; +import { addTripService } from '../../services/trip/trip.service'; import * as validator from '@packrat/validations'; -export const addTrip = async (c) => { +export const addTrip = async (c: Context) => { try { - const tripData = await c.req.json(); - const trip = await addTripService(tripData); + const requestData = (await c.req.json()) satisfies validator.AddTripType; + const tripData = { ...requestData, ownerId: c.user.id }; + const trip = await addTripService(tripData, c.executionCtx); return c.json(trip, 200); } catch (error) { return c.json({ error: `${error.message}` }, 500); @@ -14,7 +16,7 @@ export const addTrip = async (c) => { export function addTripRoute() { return protectedProcedure.input(validator.addTrip).mutation(async (opts) => { - const tripData = opts.input; + const tripData = { ownerId: opts.ctx.user.id, ...opts.input }; return await addTripService(tripData, opts.ctx.executionCtx); }); } diff --git a/server/src/controllers/trip/editTrip.ts b/server/src/controllers/trip/editTrip.ts index a509c8391..e84403a2c 100644 --- a/server/src/controllers/trip/editTrip.ts +++ b/server/src/controllers/trip/editTrip.ts @@ -1,12 +1,12 @@ import { protectedProcedure } from '../../trpc'; import * as validator from '@packrat/validations'; -import { Trip } from '../../drizzle/methods/trip'; +import { editTripService } from '../../services/trip/trip.service'; +import { Context } from 'hono'; -export const editTrip = async (c) => { +export const editTrip = async (c: Context) => { try { const tripData = await c.req.json(); - const tripClass = new Trip(); - const trip = await tripClass.update(tripData); + const trip = await editTripService(tripData, c.executionCtx); return c.json({ trip }, 200); } catch (error) { return c.json({ error: `${error.message}` }, 500); @@ -16,8 +16,7 @@ export const editTrip = async (c) => { export function editTripRoute() { return protectedProcedure.input(validator.editTrip).mutation(async (opts) => { const tripData = { ...opts.input }; - const tripClass = new Trip(); - const trip = await tripClass.update(tripData); + const trip = await editTripService(tripData, opts.ctx.executionCtx); return trip; }); } diff --git a/server/src/controllers/trip/index.ts b/server/src/controllers/trip/index.ts index 85037928d..ff3db090a 100644 --- a/server/src/controllers/trip/index.ts +++ b/server/src/controllers/trip/index.ts @@ -4,3 +4,4 @@ export * from './editTrip'; export * from './getPublicTrips'; export * from './getTrip'; export * from './getTripById'; +export * from './setTripVisibility'; diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts index d1d1a39b2..c03b5e4f1 100644 --- a/server/src/db/schema.ts +++ b/server/src/db/schema.ts @@ -403,7 +403,8 @@ export const trip = sqliteTable('trip', { .primaryKey() .$defaultFn(() => createId()), name: text('name').notNull(), - description: text('description').notNull(), + description: text('description'), + // destination: text('destination').notNull(), parks: text('parks', { mode: 'json' }).$type< Array<{ id: string; name: string }> >(), @@ -412,7 +413,6 @@ export const trip = sqliteTable('trip', { >(), start_date: text('start_date').notNull(), end_date: text('end_date').notNull(), - destination: text('destination').notNull(), owner_id: text('owner_id').references(() => user.id, { onDelete: 'cascade', }), diff --git a/server/src/drizzle/methods/Geojson.ts b/server/src/drizzle/methods/Geojson.ts index def55775c..ea7e2b4d6 100644 --- a/server/src/drizzle/methods/Geojson.ts +++ b/server/src/drizzle/methods/Geojson.ts @@ -1,3 +1,4 @@ +import { eq } from 'drizzle-orm'; import { DbClient } from '../../db/client'; import { type InsertGeoJson, geojson } from '../../db/schema'; @@ -5,14 +6,29 @@ export class GeoJson { async create(geoJSON: InsertGeoJson) { try { const db = DbClient.instance; - const record = await db - .insert(geojson) - .values({ geoJSON }) - .returning() - .get(); + const record = await db.insert(geojson).values(geoJSON).returning().get(); return record; } catch (error) { throw new Error(`Failed to create geojson record: ${error.message}`); } } + + async update( + id: string, + data: Partial, + filter = eq(geojson.id, id), + ) { + try { + const geojsonValue = await DbClient.instance + .update(geojson) + .set(data) + .where(filter) + .returning() + .get(); + + return geojsonValue; + } catch (error) { + throw new Error(`Failed to geojson record: ${error.message}`); + } + } } diff --git a/server/src/drizzle/methods/trip.ts b/server/src/drizzle/methods/trip.ts index e032bf1c6..fb047acbb 100644 --- a/server/src/drizzle/methods/trip.ts +++ b/server/src/drizzle/methods/trip.ts @@ -1,6 +1,10 @@ import { eq } from 'drizzle-orm'; import { DbClient } from '../../db/client'; -import { type InsertTrip, trip as TripTable } from '../../db/schema'; +import { + type InsertTrip, + type Trip as SelectTrip, + trip as TripTable, +} from '../../db/schema'; export class Trip { async update(trip: Partial) { @@ -48,7 +52,7 @@ export class Trip { async findById(id: string) { try { - const trip = await DbClient.instance.query.trip.findFirst({ + return DbClient.instance.query.trip.findFirst({ where: eq(TripTable.id, id), with: { owner: { @@ -59,27 +63,27 @@ export class Trip { }, packs: { columns: { id: true, name: true }, - with: { - itemPacks: { - columns: { packId: true }, - with: { - item: { - columns: { - id: true, - name: true, - weight: true, - quantity: true, - unit: true, - }, - with: { - category: { - columns: { id: true, name: true }, - }, - }, - }, - }, - }, - }, + // with: { + // itemPacks: { + // columns: { packId: true }, + // with: { + // item: { + // columns: { + // id: true, + // name: true, + // weight: true, + // quantity: true, + // unit: true, + // }, + // with: { + // category: { + // columns: { id: true, name: true }, + // }, + // }, + // }, + // }, + // }, + // }, }, tripGeojsons: { columns: {}, @@ -89,7 +93,6 @@ export class Trip { }, }, }); - return trip; } catch (error) { throw new Error(`Failed to find trip by id: ${error.message}`); } diff --git a/server/src/routes/trpcRouter.ts b/server/src/routes/trpcRouter.ts index 165874968..4f3b87c60 100644 --- a/server/src/routes/trpcRouter.ts +++ b/server/src/routes/trpcRouter.ts @@ -29,6 +29,7 @@ import { getPublicTripsRoute, getTripByIdRoute, getTripsRoute, + setTripVisibilityRoute, } from '../controllers/trip'; import { addTemplateRoute, @@ -111,6 +112,7 @@ import { import { router as trpcRouter } from '../trpc'; import { getOfflineMapsRoute, saveOfflineMapRoute } from '../modules/map'; +import { setTripVisibility } from '@packrat/validations'; export const appRouter = trpcRouter({ getUserById: getUserByIdRoute(), @@ -144,6 +146,7 @@ export const appRouter = trpcRouter({ getTripById: getTripByIdRoute(), addTrip: addTripRoute(), editTrip: editTripRoute(), + setTripVisibility: setTripVisibilityRoute(), deleteTrip: deleteTripRoute(), // templates routes getPackTemplates: getPackTemplatesRoute(), diff --git a/server/src/services/trip/addTripService.ts b/server/src/services/trip/addTripService.ts index cece53d6d..c572a634d 100644 --- a/server/src/services/trip/addTripService.ts +++ b/server/src/services/trip/addTripService.ts @@ -1,3 +1,4 @@ +import * as validator from '@packrat/validations'; import { calculateTripScore } from 'src/utils/scoreTrip'; import { GeoJson } from '../../drizzle/methods/Geojson'; import { TripGeoJson } from '../../drizzle/methods/TripGeoJson'; @@ -5,35 +6,48 @@ import { Trip } from '../../drizzle/methods/trip'; import { validateGeojsonId, validateGeojsonType } from '../../utils/geojson'; import { GeojsonStorageService } from '../geojsonStorage'; import { scoreTripService } from './scoreTripService'; +import { trip } from 'src/db/schema'; export const addTripService = async ( - tripData: any, + tripData: validator.AddTripType & { ownerId: string }, executionCtx: ExecutionContext, ) => { try { - const { geoJSON, ...otherTripData } = tripData; + const geoJSON = tripData.geoJSON; const tripClass = new Trip(); + // Create Trip const newTrip = await tripClass.create({ - ...otherTripData, - trails: otherTripData.trails ? JSON.parse(otherTripData.trails) : null, - parks: otherTripData.parks ? JSON.parse(otherTripData.parks) : null, + name: tripData.name, + description: tripData.description, + start_date: tripData.start_date, + end_date: tripData.end_date, + activity: tripData.activity, + owner_id: tripData.ownerId, + is_public: false, + trails: tripData.trails ? JSON.parse(tripData.trails) : null, + parks: tripData.parks ? JSON.parse(tripData.parks) : null, }); + await scoreTripService(newTrip.id); + const geojsonClass = new GeoJson(); const tripGeoJsonClass = new TripGeoJson(); if (!geoJSON) { throw new Error("Geojson data doesn't exist"); } - // const insertedGeoJson = await geojsonClass.create(geoJSON); - // await tripGeoJsonClass.create({ - // tripId: newTrip.id, - // geojsonId: insertedGeoJson.id, - // }); + const insertedGeoJson = await geojsonClass.create({ + geoJSON, + }); + + await tripGeoJsonClass.create({ + tripId: newTrip.id, + geojsonId: insertedGeoJson.id, + }); executionCtx.waitUntil( - GeojsonStorageService.save('trip', JSON.stringify(geoJSON), newTrip.id), + GeojsonStorageService.save('trip', geoJSON, newTrip.id), ); return newTrip; diff --git a/server/src/services/trip/trip.service.ts b/server/src/services/trip/trip.service.ts index 270d8768f..d40c87337 100644 --- a/server/src/services/trip/trip.service.ts +++ b/server/src/services/trip/trip.service.ts @@ -1,4 +1,5 @@ export * from './addTripService'; +export * from './editTripService'; export * from './getPublicTripService'; export * from './getTripByIdService'; export * from './getTripsService'; From 3c179129986c17dfc938f4fc2a05376207d8552a Mon Sep 17 00:00:00 2001 From: Tadjaur Date: Sun, 15 Dec 2024 11:09:40 +0100 Subject: [PATCH 2/4] --amend --- .../src/controllers/trip/setTripVisibility.ts | 12 +++ server/src/db/schema.ts | 2 - server/src/services/trip/editTripService.ts | 79 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 server/src/controllers/trip/setTripVisibility.ts create mode 100644 server/src/services/trip/editTripService.ts diff --git a/server/src/controllers/trip/setTripVisibility.ts b/server/src/controllers/trip/setTripVisibility.ts new file mode 100644 index 000000000..c3f8ed013 --- /dev/null +++ b/server/src/controllers/trip/setTripVisibility.ts @@ -0,0 +1,12 @@ +import { protectedProcedure } from '../../trpc'; +import * as validator from '@packrat/validations'; +import { setTripVisibilityService } from '../../services/trip/trip.service'; + +export function setTripVisibilityRoute() { + return protectedProcedure + .input(validator.setTripVisibility) + .mutation(async (opts) => { + const trip = await setTripVisibilityService(opts.input); + return trip; + }); +} diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts index c03b5e4f1..9ff007b19 100644 --- a/server/src/db/schema.ts +++ b/server/src/db/schema.ts @@ -404,7 +404,6 @@ export const trip = sqliteTable('trip', { .$defaultFn(() => createId()), name: text('name').notNull(), description: text('description'), - // destination: text('destination').notNull(), parks: text('parks', { mode: 'json' }).$type< Array<{ id: string; name: string }> >(), @@ -462,7 +461,6 @@ export const tripRelations = relations(trip, ({ one, many }) => ({ fields: [trip.pack_id], references: [pack.id], }), - // geojsons: many(tripGeojsons), tripGeojsons: many(tripGeojsons), })); diff --git a/server/src/services/trip/editTripService.ts b/server/src/services/trip/editTripService.ts new file mode 100644 index 000000000..9152190ef --- /dev/null +++ b/server/src/services/trip/editTripService.ts @@ -0,0 +1,79 @@ +import * as validator from '@packrat/validations'; +import { GeoJson } from '../../drizzle/methods/Geojson'; +import { TripGeoJson } from '../../drizzle/methods/TripGeoJson'; +import { Trip } from '../../drizzle/methods/trip'; +import { GeojsonStorageService } from '../geojsonStorage'; +import { scoreTripService } from './scoreTripService'; + +export const editTripService = async ( + tripData: validator.EditTripType, + executionCtx: ExecutionContext, +) => { + try { + const tripClass = new Trip(); + const selectedTrip = await tripClass.findById(tripData.id); + const updatedTrip = await tripClass.update({ + name: tripData.name || selectedTrip.name, + description: tripData.description || selectedTrip.description, + start_date: tripData.start_date || selectedTrip.start_date, + end_date: tripData.end_date || selectedTrip.end_date, + activity: tripData.activity || selectedTrip.activity, + trails: tripData.trails + ? JSON.parse(tripData.trails) + : selectedTrip.trails, + parks: tripData.parks ? JSON.parse(tripData.parks) : selectedTrip.parks, + }); + + await scoreTripService(selectedTrip.id); + + const serializedGeoJSON = tripData.geoJSON; + if (!serializedGeoJSON) { + return updatedTrip; + } + + const geojsonClass = new GeoJson(); + + const tripGeoJSONs = selectedTrip.tripGeojsons; + if (tripGeoJSONs.length > 0) { + await geojsonClass.update(tripGeoJSONs[0].geojson.id, { + geoJSON: serializedGeoJSON, + }); + } else { + const insertedGeoJson = await geojsonClass.create({ + geoJSON: serializedGeoJSON, + }); + + const tripGeoJsonClass = new TripGeoJson(); + await tripGeoJsonClass.create({ + tripId: selectedTrip.id, + geojsonId: insertedGeoJson.id, + }); + } + + executionCtx.waitUntil( + GeojsonStorageService.save('trip', serializedGeoJSON, selectedTrip.id), + ); + + return updatedTrip; + } catch (error) { + console.error(error); + throw new Error('Unable to add trip and GeoJSON data'); + } +}; + +export const setTripVisibilityService = async ( + tripData: validator.SetTripVisibilityType, +) => { + try { + const tripClass = new Trip(); + const selectedTrip = await tripClass.findById(tripData.id); + const updatedTrip = await tripClass.update({ + id: selectedTrip.id, + is_public: tripData.is_public === '1', + }); + return updatedTrip; + } catch (error) { + console.error(error); + throw new Error('Unable to add trip and GeoJSON data'); + } +}; From 5e9251c095b13bd1935f1a616d087fc409a68a39 Mon Sep 17 00:00:00 2001 From: Tadjaur Date: Sun, 15 Dec 2024 11:43:39 +0100 Subject: [PATCH 3/4] Add bounds as optional in the request --- .../tripRoutesValidator.ts | 26 +++++++++---------- server/src/services/trip/addTripService.ts | 5 +++- server/src/services/trip/editTripService.ts | 3 +++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts b/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts index 960554e0e..c3134bba6 100644 --- a/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts +++ b/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts @@ -7,7 +7,7 @@ export const addTripForm = z.object({ name: z.string(), description: z.string().optional().nullable(), activity: z.enum(tripActivityValues).optional(), - is_public: z.union([z.literal('0'), z.literal('1')]), + is_public: z.union([z.literal('0'), z.literal('1')]).optional(), }); const coordinateSchema = z.lazy(() => @@ -47,31 +47,31 @@ export const getTripById = z.object({ }); export const addTripDetails = z.object({ - start_date: z.string(), - end_date: z.string(), activity: z.enum(tripActivityValues).optional(), + bounds: z.tuple([z.array(z.number()), z.array(z.number())]).optional(), + end_date: z.string(), + geoJSON: z.string(), + packId: z.string(), parks: z.string().optional(), + start_date: z.string(), trails: z.string().optional(), - packId: z.string(), - geoJSON: z.string(), }); -export const addTrip = addTripDetails.merge(addTripForm).omit({ - is_public: true, -}); +export const addTrip = addTripDetails.merge(addTripForm); export type AddTripType = z.infer; export const editTrip = z.object({ - id: z.string().min(1), - name: z.string().optional(), + activity: z.enum(tripActivityValues).optional(), + bounds: z.array(z.array(z.number())).length(2).optional(), description: z.string().optional(), - start_date: z.string().optional(), end_date: z.string().optional(), + geoJSON: z.string().optional(), + name: z.string().optional(), packId: z.string().optional(), parks: z.string().optional(), + start_date: z.string().optional(), trails: z.string().optional(), - activity: z.enum(tripActivityValues).optional(), - geoJSON: z.string().optional(), + id: z.string().min(1), }); export type EditTripType = z.infer; diff --git a/server/src/services/trip/addTripService.ts b/server/src/services/trip/addTripService.ts index c572a634d..de3d09a12 100644 --- a/server/src/services/trip/addTripService.ts +++ b/server/src/services/trip/addTripService.ts @@ -24,9 +24,12 @@ export const addTripService = async ( end_date: tripData.end_date, activity: tripData.activity, owner_id: tripData.ownerId, - is_public: false, + is_public: tripData.is_public === '0', trails: tripData.trails ? JSON.parse(tripData.trails) : null, parks: tripData.parks ? JSON.parse(tripData.parks) : null, + ...(tripData.bounds && { + bounds: [tripData.bounds[0], tripData.bounds[1]], + }), }); await scoreTripService(newTrip.id); diff --git a/server/src/services/trip/editTripService.ts b/server/src/services/trip/editTripService.ts index 9152190ef..78bdedbd9 100644 --- a/server/src/services/trip/editTripService.ts +++ b/server/src/services/trip/editTripService.ts @@ -22,6 +22,9 @@ export const editTripService = async ( ? JSON.parse(tripData.trails) : selectedTrip.trails, parks: tripData.parks ? JSON.parse(tripData.parks) : selectedTrip.parks, + ...(tripData.bounds && { + bounds: [tripData.bounds[0], tripData.bounds[1]], + }), }); await scoreTripService(selectedTrip.id); From c349373b4d9004ba968d61d842e3e82e555d2783 Mon Sep 17 00:00:00 2001 From: Tadjaur Date: Sun, 15 Dec 2024 19:31:36 +0100 Subject: [PATCH 4/4] Edit schema --- .../tripRoutesValidator.ts | 20 ++++++------------- server/src/drizzle/methods/trip.ts | 2 +- server/src/services/trip/addTripService.ts | 6 ++---- server/src/services/trip/editTripService.ts | 1 + 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts b/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts index c3134bba6..0a3d5c1de 100644 --- a/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts +++ b/packages/validations/src/validations/tripRoutesValidator/tripRoutesValidator.ts @@ -51,7 +51,7 @@ export const addTripDetails = z.object({ bounds: z.tuple([z.array(z.number()), z.array(z.number())]).optional(), end_date: z.string(), geoJSON: z.string(), - packId: z.string(), + pack_id: z.string(), parks: z.string().optional(), start_date: z.string(), trails: z.string().optional(), @@ -60,19 +60,11 @@ export const addTripDetails = z.object({ export const addTrip = addTripDetails.merge(addTripForm); export type AddTripType = z.infer; -export const editTrip = z.object({ - activity: z.enum(tripActivityValues).optional(), - bounds: z.array(z.array(z.number())).length(2).optional(), - description: z.string().optional(), - end_date: z.string().optional(), - geoJSON: z.string().optional(), - name: z.string().optional(), - packId: z.string().optional(), - parks: z.string().optional(), - start_date: z.string().optional(), - trails: z.string().optional(), - id: z.string().min(1), -}); +export const editTrip = addTrip.merge( + z.object({ + id: z.string().min(1), + }), +); export type EditTripType = z.infer; diff --git a/server/src/drizzle/methods/trip.ts b/server/src/drizzle/methods/trip.ts index fb047acbb..8e242e100 100644 --- a/server/src/drizzle/methods/trip.ts +++ b/server/src/drizzle/methods/trip.ts @@ -7,7 +7,7 @@ import { } from '../../db/schema'; export class Trip { - async update(trip: Partial) { + async update(trip: InsertTrip) { try { if (!trip.id) { throw new Error('Trip id is required for update operation'); diff --git a/server/src/services/trip/addTripService.ts b/server/src/services/trip/addTripService.ts index de3d09a12..286368252 100644 --- a/server/src/services/trip/addTripService.ts +++ b/server/src/services/trip/addTripService.ts @@ -1,12 +1,9 @@ import * as validator from '@packrat/validations'; -import { calculateTripScore } from 'src/utils/scoreTrip'; import { GeoJson } from '../../drizzle/methods/Geojson'; import { TripGeoJson } from '../../drizzle/methods/TripGeoJson'; import { Trip } from '../../drizzle/methods/trip'; -import { validateGeojsonId, validateGeojsonType } from '../../utils/geojson'; import { GeojsonStorageService } from '../geojsonStorage'; import { scoreTripService } from './scoreTripService'; -import { trip } from 'src/db/schema'; export const addTripService = async ( tripData: validator.AddTripType & { ownerId: string }, @@ -22,8 +19,9 @@ export const addTripService = async ( description: tripData.description, start_date: tripData.start_date, end_date: tripData.end_date, - activity: tripData.activity, + activity: tripData.activity || 'trip', owner_id: tripData.ownerId, + pack_id: tripData.pack_id, is_public: tripData.is_public === '0', trails: tripData.trails ? JSON.parse(tripData.trails) : null, parks: tripData.parks ? JSON.parse(tripData.parks) : null, diff --git a/server/src/services/trip/editTripService.ts b/server/src/services/trip/editTripService.ts index 78bdedbd9..f7083bb43 100644 --- a/server/src/services/trip/editTripService.ts +++ b/server/src/services/trip/editTripService.ts @@ -18,6 +18,7 @@ export const editTripService = async ( start_date: tripData.start_date || selectedTrip.start_date, end_date: tripData.end_date || selectedTrip.end_date, activity: tripData.activity || selectedTrip.activity, + pack_id: tripData.pack_id || selectedTrip.pack_id, trails: tripData.trails ? JSON.parse(tripData.trails) : selectedTrip.trails,