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

update driver api #232

Merged
merged 6 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
28 changes: 22 additions & 6 deletions e2e/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ test('Set ticket checked', async ({ page }) => {
const ticketCode = 'a7d421840cf89e052d7c1aa74caf66d8';
await execSQL(sql`UPDATE "request" SET ticket_code = ${ticketCode} WHERE id = ${requestId}`);

const response = await page
const response1 = await page
.context()
.request.post(`/api/driver/ticket?requestId=${requestId}&ticketCode=${ticketCode}`);
expect(response.status()).toBe(200);
.request.put(`/api/driver/ticket?requestId=${requestId}&ticketCode=${ticketCode}`);
expect(response1.status()).toBe(204);

const response2 = await page
.context()
.request.put(`/api/driver/ticket?requestId="NaN"&ticketCode=${ticketCode}`);
expect(response2.status()).toBe(400);

const response3 = await page
.context()
.request.put(`/api/driver/ticket?requestId=${requestId}&ticketCode=invalidCode`);
expect(response3.status()).toBe(404);
});

test('Set tour fare', async ({ page }) => {
Expand All @@ -80,8 +90,14 @@ test('Set tour fare', async ({ page }) => {

const fare = 1234;

const response = await page
const response1 = await page
.context()
.request.post(`/api/driver/fare?tourId=${tourId}&fare=${fare}`);
expect(response.status()).toBe(200);
.request.put(`/api/driver/fare?tourId=${tourId}&fare=${fare}`);
expect(response1.status()).toBe(204);

const response2 = await page.context().request.put(`/api/driver/fare?tourId=NaN&fare=${fare}`);
expect(response2.status()).toBe(400);

const response3 = await page.context().request.put(`/api/driver/fare?tourId=${tourId}&fare=NaN`);
expect(response3.status()).toBe(400);
});
18 changes: 13 additions & 5 deletions src/routes/api/driver/fare/+server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { db } from '$lib/server/db';
import { readInt } from '$lib/server/util/readForm.js';
import { json } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';

export const POST = async ({ url }) => {
export const PUT = async ({ url }) => {
const tourId = readInt(url.searchParams.get('tourId'));
const fare = readInt(url.searchParams.get('fare'));

if (isNaN(fare) || isNaN(tourId)) {
throw 'bad params';
throw error(400, 'Bad request');
}

await db.updateTable('tour').set({ fare: fare }).where('id', '=', tourId).execute();
const result = await db
.updateTable('tour')
.set({ fare: fare })
.where('id', '=', tourId)
.executeTakeFirst();

return json({ success: true });
if (result.numUpdatedRows === BigInt(0)) {
throw error(404, 'Not found');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return new Response(null, { status: 204 });
};
16 changes: 10 additions & 6 deletions src/routes/api/driver/ticket/+server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { db } from '$lib/server/db';
import { readInt } from '$lib/server/util/readForm.js';
import { json } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';

export const POST = async ({ url }) => {
export const PUT = async ({ url }) => {
const requestId = readInt(url.searchParams.get('requestId'));
const ticketCode = url.searchParams.get('ticketCode');

if (typeof ticketCode !== 'string' || isNaN(requestId)) {
throw 'bad params';
throw error(400, 'Bad request');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

await db
const result = await db
.updateTable('request')
.set({ ticketChecked: true })
.where('id', '=', requestId)
.where('ticketCode', '=', ticketCode)
.execute();
.executeTakeFirst();

return json({ success: true });
if (result.numUpdatedRows === BigInt(0)) {
throw error(404, 'Not found');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error
Throws an error with a HTTP status code and an optional message. When called during request handling, this will cause SvelteKit to return an error response without invoking handleError. Make sure you’re not catching the thrown error, which would prevent SvelteKit from handling it.
https://svelte.dev/docs/kit/@sveltejs-kit#error

}

return new Response(null, { status: 204 });
};
7 changes: 6 additions & 1 deletion src/routes/api/driver/tour/+server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { getTours } from '$lib/server/db/getTours';
import { readInt } from '$lib/server/util/readForm.js';
import { json } from '@sveltejs/kit';
import { json, error } from '@sveltejs/kit';

export const GET = async ({ locals, url }) => {
const companyId = locals.session!.companyId!;
const fromTime = readInt(url.searchParams.get('fromTime'));
const toTime = readInt(url.searchParams.get('toTime'));

if (isNaN(fromTime) || isNaN(toTime)) {
throw error(400, 'Bad request');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return json(await getTours(false, companyId, [fromTime, toTime]));
};