Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate entry's sheet collection when delete #45

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Changes from 1 commit
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
53 changes: 21 additions & 32 deletions src/entry/controller/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
BeckyPollard marked this conversation as resolved.
Show resolved Hide resolved
person,
+ctx.params.entryId,
)
await entryService.findById(person, +ctx.params.entryId)
);
}

async put(ctx: Context) {

ctx.request.validate<EntrySchema>('https://tt.badgateway.net/schema/entry.json');

const person = await personService.findById(
+ctx.params.personId,
ctx.request.validate<EntrySchema>(
'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;

Expand All @@ -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);
Expand All @@ -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();