From 9b7397947dcf2bf73a0f50766423bcdce84b2f82 Mon Sep 17 00:00:00 2001 From: BeckyPollard Date: Tue, 25 Oct 2022 16:28:07 -0400 Subject: [PATCH 1/3] Update sheet collection for deleted entry --- src/entry/controller/item.ts | 53 ++++++++++++++---------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/src/entry/controller/item.ts b/src/entry/controller/item.ts index 1949e2c..919cc9a 100644 --- a/src/entry/controller/item.ts +++ b/src/entry/controller/item.ts @@ -6,39 +6,28 @@ import * as entryService from '../service'; import * as personService from '../../person/service'; import * as projectService from '../../project/service'; import * as hal from '../formats/hal'; - -import { Entry as EntrySchema } from '@badgateway/tt-types'; +import {DateTime} from 'luxon'; +import {Entry as EntrySchema} from '@badgateway/tt-types'; class Entry extends Controller { - async get(ctx: Context) { - ctx.response.type = 'application/hal+json'; - const person = await personService.findById( - +ctx.params.personId, - ); + const person = await personService.findById(+ctx.params.personId); ctx.response.body = hal.item( - await entryService.findById( - person, - +ctx.params.entryId, - ) + await entryService.findById(person, +ctx.params.entryId) ); } async put(ctx: Context) { - - ctx.request.validate('https://tt.badgateway.net/schema/entry.json'); - - const person = await personService.findById( - +ctx.params.personId, + ctx.request.validate( + 'https://tt.badgateway.net/schema/entry.json' ); - const entry = await entryService.findById( - person, - +ctx.params.entryId, - ); + const person = await personService.findById(+ctx.params.personId); + + const entry = await entryService.findById(person, +ctx.params.entryId); const body = ctx.request.body; @@ -48,9 +37,11 @@ class Entry extends Controller { throw new BadRequest('A link with rel "project" must be provided'); } - const projectId = (projectUrl.href.split('/').pop()); + const projectId = projectUrl.href.split('/').pop(); if (!projectId) { - throw new BadRequest('The project link must be in the format /projects/123'); + throw new BadRequest( + 'The project link must be in the format /projects/123' + ); } const project = await projectService.findById(+projectId); @@ -63,29 +54,27 @@ class Entry extends Controller { await entryService.update(entry); ctx.status = 204; - } async delete(ctx: Context) { + const person = await personService.findById(+ctx.params.personId); - const person = await personService.findById( - +ctx.params.personId, - ); + const entry = await entryService.findById(person, +ctx.params.entryId); - const entry = await entryService.findById( - person, - +ctx.params.entryId, - ); + const entryDate = DateTime.fromISO(entry.date); await entryService.deleteEntry(entry); ctx.response.status = 204; ctx.response.links.add({ rel: 'invalidates', - href: `/person/${person.id}/entry` + href: `/person/${person.id}/entry`, + }); + ctx.response.links.add({ + rel: 'invalidates', + href: `/person/${person.id}/sheet/${entryDate.year}/${entryDate.weekNumber}`, }); } - } export default new Entry(); From 05c8a3fa56b5aa4441177a0bf73924e7e870258f Mon Sep 17 00:00:00 2001 From: BeckyPollard Date: Tue, 25 Oct 2022 17:10:00 -0400 Subject: [PATCH 2/3] Undo unintended prettier formatting --- src/entry/controller/item.ts | 47 ++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/entry/controller/item.ts b/src/entry/controller/item.ts index 919cc9a..5c423ac 100644 --- a/src/entry/controller/item.ts +++ b/src/entry/controller/item.ts @@ -7,27 +7,38 @@ import * as personService from '../../person/service'; import * as projectService from '../../project/service'; import * as hal from '../formats/hal'; import {DateTime} from 'luxon'; -import {Entry as EntrySchema} from '@badgateway/tt-types'; +import { Entry as EntrySchema } from '@badgateway/tt-types'; class Entry extends Controller { + async get(ctx: Context) { + ctx.response.type = 'application/hal+json'; - const person = await personService.findById(+ctx.params.personId); + const person = await personService.findById( + +ctx.params.personId, + ); ctx.response.body = hal.item( - await entryService.findById(person, +ctx.params.entryId) + await entryService.findById( + person, + +ctx.params.entryId, + ) ); } async put(ctx: Context) { - ctx.request.validate( - 'https://tt.badgateway.net/schema/entry.json' - ); - const person = await personService.findById(+ctx.params.personId); + ctx.request.validate('https://tt.badgateway.net/schema/entry.json'); + + const person = await personService.findById( + +ctx.params.personId, + ); - const entry = await entryService.findById(person, +ctx.params.entryId); + const entry = await entryService.findById( + person, + +ctx.params.entryId, + ); const body = ctx.request.body; @@ -37,11 +48,9 @@ class Entry extends Controller { throw new BadRequest('A link with rel "project" must be provided'); } - const projectId = projectUrl.href.split('/').pop(); + const projectId = (projectUrl.href.split('/').pop()); if (!projectId) { - throw new BadRequest( - 'The project link must be in the format /projects/123' - ); + throw new BadRequest('The project link must be in the format /projects/123'); } const project = await projectService.findById(+projectId); @@ -54,27 +63,35 @@ class Entry extends Controller { await entryService.update(entry); ctx.status = 204; + } async delete(ctx: Context) { - const person = await personService.findById(+ctx.params.personId); - const entry = await entryService.findById(person, +ctx.params.entryId); + const person = await personService.findById( + +ctx.params.personId, + ); const entryDate = DateTime.fromISO(entry.date); + const entry = await entryService.findById( + person, + +ctx.params.entryId, + ); + await entryService.deleteEntry(entry); ctx.response.status = 204; ctx.response.links.add({ rel: 'invalidates', - href: `/person/${person.id}/entry`, + href: `/person/${person.id}/entry` }); ctx.response.links.add({ rel: 'invalidates', href: `/person/${person.id}/sheet/${entryDate.year}/${entryDate.weekNumber}`, }); } + } export default new Entry(); From ca935f154710b981113158bff288d5a692773cbb Mon Sep 17 00:00:00 2001 From: BeckyPollard Date: Tue, 25 Oct 2022 17:11:43 -0400 Subject: [PATCH 3/3] entryDate on correct line --- src/entry/controller/item.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entry/controller/item.ts b/src/entry/controller/item.ts index 5c423ac..64f3ab6 100644 --- a/src/entry/controller/item.ts +++ b/src/entry/controller/item.ts @@ -72,13 +72,13 @@ class Entry extends Controller { +ctx.params.personId, ); - const entryDate = DateTime.fromISO(entry.date); - const entry = await entryService.findById( person, +ctx.params.entryId, ); + const entryDate = DateTime.fromISO(entry.date); + await entryService.deleteEntry(entry); ctx.response.status = 204;