mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
fix(packing): apply templates into the active list tab (#1581)
This commit is contained in:
committed by
GitHub
parent
2e05126c6a
commit
5e686eac06
@@ -348,15 +348,17 @@ export class PackingController {
|
||||
@CurrentUser() user: User,
|
||||
@Param('tripId') tripId: string,
|
||||
@Param('templateId') templateId: string,
|
||||
@Body() body: { visibility?: 'common' | 'personal' },
|
||||
@Headers('x-socket-id') socketId?: string,
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
this.requireEdit(trip, user);
|
||||
const added = this.packing.applyTemplate(tripId, templateId);
|
||||
const visibility = body?.visibility === 'personal' ? 'personal' : 'common';
|
||||
const added = this.packing.applyTemplate(tripId, templateId, visibility, user.id);
|
||||
if (!added) {
|
||||
throw new HttpException({ error: 'Template not found or empty' }, 404);
|
||||
}
|
||||
this.packing.broadcast(tripId, 'packing:template-applied', { items: added }, socketId);
|
||||
this.packing.broadcastItem(tripId, 'packing:template-applied', { items: added }, added[0], socketId);
|
||||
return { items: added, count: added.length };
|
||||
}
|
||||
|
||||
|
||||
@@ -126,8 +126,8 @@ export class PackingService {
|
||||
return svc.listTemplates();
|
||||
}
|
||||
|
||||
applyTemplate(tripId: string, templateId: string) {
|
||||
return svc.applyTemplate(tripId, templateId);
|
||||
applyTemplate(tripId: string, templateId: string, visibility: 'common' | 'personal', ownerId: number) {
|
||||
return svc.applyTemplate(tripId, templateId, visibility, ownerId);
|
||||
}
|
||||
|
||||
saveAsTemplate(tripId: string, userId: number, name: string) {
|
||||
|
||||
@@ -390,7 +390,12 @@ export function listTemplates() {
|
||||
|
||||
// ── Apply Template ─────────────────────────────────────────────────────────
|
||||
|
||||
export function applyTemplate(tripId: string | number, templateId: string | number) {
|
||||
export function applyTemplate(
|
||||
tripId: string | number,
|
||||
templateId: string | number,
|
||||
visibility: 'common' | 'personal' = 'common',
|
||||
ownerId?: number,
|
||||
) {
|
||||
const templateItems = db.prepare(`
|
||||
SELECT ti.name, tc.name as category
|
||||
FROM packing_template_items ti
|
||||
@@ -403,11 +408,13 @@ export function applyTemplate(tripId: string | number, templateId: string | numb
|
||||
|
||||
const maxOrder = db.prepare('SELECT MAX(sort_order) as max FROM packing_items WHERE trip_id = ?').get(tripId) as { max: number | null };
|
||||
let sortOrder = (maxOrder.max !== null ? maxOrder.max : -1) + 1;
|
||||
const isPrivate = ownerId != null ? visibilityToPrivate(visibility) : 0;
|
||||
const owner = isPrivate ? ownerId! : null;
|
||||
|
||||
const insert = db.prepare('INSERT INTO packing_items (trip_id, name, checked, category, sort_order, updated_at) VALUES (?, ?, 0, ?, ?, CURRENT_TIMESTAMP)');
|
||||
const insert = db.prepare('INSERT INTO packing_items (trip_id, name, checked, category, sort_order, is_private, owner_id, updated_at) VALUES (?, ?, 0, ?, ?, ?, ?, CURRENT_TIMESTAMP)');
|
||||
const added: any[] = [];
|
||||
for (const ti of templateItems) {
|
||||
const result = insert.run(tripId, ti.name, ti.category, sortOrder++);
|
||||
const result = insert.run(tripId, ti.name, ti.category, sortOrder++, isPrivate, owner);
|
||||
const item = db.prepare('SELECT * FROM packing_items WHERE id = ?').get(result.lastInsertRowid);
|
||||
added.push(item);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user