Skip to content

Commit

Permalink
feat: let the user know which request they created
Browse files Browse the repository at this point in the history
When creating a new request, the API currently doesn't return any information when it succeeds. This change adds the request itself to the response, so the user doesn't have to look it up separately. This also then matches other endpoints.

Refs #425
  • Loading branch information
thewilkybarkid committed Nov 12, 2021
1 parent 9be1fc1 commit 99280e1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
18 changes: 14 additions & 4 deletions integration/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const rapidReviewSchema = z.object({
uuid: z.string(),
});

const requestSchema = z.object({
uuid: z.string(),
})

const templateSchema = z.object({
uuid: z.string(),
title: z.string(),
Expand Down Expand Up @@ -75,6 +79,8 @@ export type Preprint = z.infer<typeof preprintSchema>;

export type RapidReview = z.infer<typeof rapidReviewSchema>;

export type Request = z.infer<typeof requestSchema>;

export type Template = z.infer<typeof templateSchema>;

export type User = z.infer<typeof userSchema>;
Expand Down Expand Up @@ -108,16 +114,16 @@ export async function ensurePreprint(
export async function ensureRequest(
fetch: Fetch,
preprint: string,
): Promise<unknown> {
): Promise<Request> {
const requests = await fetch(`/api/v2/preprints/${preprint}/requests`, {
headers: adminHeaders,
})
.then(response => response.json())
.then(dataSchema(z.array(z.unknown())).parse)
.then(dataSchema(z.array(requestSchema)).parse)
.then(response => response.data);

if (requests.length > 0) {
return;
return requests[0];
}

return await fetch(`/api/v2/preprints/${preprint}/requests`, {
Expand All @@ -127,7 +133,11 @@ export async function ensureRequest(
'Content-Type': 'application/json',
...adminHeaders,
},
}).then(ensureSuccess);
})
.then(ensureSuccess)
.then(response => response.json())
.then(dataSchema(requestSchema).parse)
.then(response => response.data);
}

export async function ensureFullReview(
Expand Down
1 change: 1 addition & 0 deletions src/backend/controllers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default function controller(reqModel, preprintModel, thisUser) {
ctx.body = {
status: 201,
message: 'created',
data: request,
};
ctx.status = 201;
};
Expand Down
51 changes: 15 additions & 36 deletions test/backend/requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,11 @@ describe('requests', () => {
expect(response.body).toStrictEqual({
message: 'created',
status: StatusCodes.CREATED,
});

const listResponse = await request(server).get(
`/api/v2/preprints/${preprint.uuid}/requests`,
);

expect(listResponse.status).toBe(StatusCodes.OK);
expect(listResponse.type).toBe('application/json');
expect(listResponse.body).toMatchObject({
data: [
{
author: user.id,
isPreprintAuthor: false,
preprint: preprint.id,
},
],
data: expect.objectContaining({
author: user.id,
isPreprintAuthor: false,
preprint: preprint.id,
}),
});
});
});
Expand All @@ -84,16 +73,11 @@ describe('author requests', () => {
expect(response.body).toStrictEqual({
message: 'created',
status: StatusCodes.CREATED,
});

const listResponse = await request(server).get(
`/api/v2/preprints/${preprint.uuid}/requests`,
);

expect(listResponse.status).toBe(StatusCodes.OK);
expect(listResponse.type).toBe('application/json');
expect(listResponse.body).toMatchObject({
data: [{ isPreprintAuthor: false }],
data: expect.objectContaining({
author: user.id,
isPreprintAuthor: false,
preprint: preprint.id,
}),
});
});

Expand All @@ -117,16 +101,11 @@ describe('author requests', () => {
expect(response.body).toStrictEqual({
message: 'created',
status: StatusCodes.CREATED,
});

const listResponse = await request(server).get(
`/api/v2/preprints/${preprint.uuid}/requests`,
);

expect(listResponse.status).toBe(StatusCodes.OK);
expect(listResponse.type).toBe('application/json');
expect(listResponse.body).toMatchObject({
data: [{ isPreprintAuthor: true }],
data: expect.objectContaining({
author: user.id,
isPreprintAuthor: true,
preprint: preprint.id,
}),
});
});
});

0 comments on commit 99280e1

Please sign in to comment.