Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion programmerbar-web/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const setup: Handle = async ({ event, resolve }) => {
event.locals.feideProvider = feideProvider;

// Setup services
event.locals.statusService = new StatusService(STATUS_KV);
event.locals.statusService = new StatusService(STATUS_KV, db);
event.locals.rateLimitService = new RateLimitService(STATUS_KV);
event.locals.magicLinkService = new MagicLinkService(STATUS_KV);
event.locals.emailService = new EmailService(sendEmail);
Expand Down
77 changes: 77 additions & 0 deletions programmerbar-web/src/lib/server/services/status.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DatabaseSync } from 'node:sqlite';
import { drizzle } from 'drizzle-orm/sqlite-proxy';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { Database } from '../db/drizzle';
import { STATUS, StatusService } from './status.service';

describe('automatic bar status', () => {
let sqlite: DatabaseSync;
let service: StatusService;

beforeEach(() => {
sqlite = new DatabaseSync(':memory:');
sqlite.exec('CREATE TABLE shift (event_id TEXT, start_at INTEGER, end_at INTEGER)');
const db = drizzle(
async (query, params) => {
const statement = sqlite.prepare(query);
statement.setReturnArrays(true);
return { rows: statement.all(...params) };
},
undefined,
{ casing: 'snake_case' }
);
const values = new Map<string, string>();
const kv = {
get: async (key: string) => values.get(key) ?? null,
put: async (key: string, value: string) => {
values.set(key, value);
},
delete: async (key: string) => {
values.delete(key);
}
} as unknown as KVNamespace;
service = new StatusService(kv, db as unknown as Database);
});

afterEach(() => sqlite.close());

function addShift(event: string, start: string, end: string) {
sqlite
.prepare('INSERT INTO shift VALUES (?, ?, ?)')
.run(event, Date.parse(start) / 1000, Date.parse(end) / 1000);
}

it('is closed without shifts', async () => {
expect(await service.get()).toBe(STATUS.CLOSED);
});

it('opens at the first start, stays open across gaps and midnight, and closes at the last end', async () => {
addShift('a', '2026-09-18T18:00:00+02:00', '2026-09-18T20:00:00+02:00');
addShift('a', '2026-09-18T21:00:00+02:00', '2026-09-19T02:00:00+02:00');
for (const [time, expected] of [
['2026-09-18T17:59:59+02:00', STATUS.CLOSED],
['2026-09-18T18:00:00+02:00', STATUS.OPEN],
['2026-09-18T20:30:00+02:00', STATUS.OPEN],
['2026-09-19T01:59:59+02:00', STATUS.OPEN],
['2026-09-19T02:00:00+02:00', STATUS.CLOSED]
] as const) {
expect(await service.get(new Date(time))).toBe(expected);
}
});

it('does not open in the gap between separate events', async () => {
addShift('a', '2026-09-18T18:00:00Z', '2026-09-18T20:00:00Z');
addShift('b', '2026-09-18T21:00:00Z', '2026-09-18T23:00:00Z');
expect(await service.get(new Date('2026-09-18T20:30:00Z'))).toBe(STATUS.CLOSED);
});

it('supports manual overrides and returning to automatic mode', async () => {
for (const status of [STATUS.OPEN, STATUS.PRIVATE, STATUS.CLOSED]) {
await service.set(status);
expect(await service.get()).toBe(status);
}
await service.setAutomatic();
expect(await service.getOverride()).toBeNull();
expect(await service.get()).toBe(STATUS.CLOSED);
});
});
37 changes: 32 additions & 5 deletions programmerbar-web/src/lib/server/services/status.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import z from 'zod';
import type { Database } from '../db/drizzle';
import { shifts } from '../db/schemas/shifts';
import { sql } from 'drizzle-orm';

export const STATUS_KEY = 'status';
// A new key makes automatic scheduling the default, ignoring the old static status.
const OVERRIDE_KEY = 'status-override';

export const STATUS = {
CLOSED: 0,
Expand All @@ -10,17 +14,40 @@ export const STATUS = {

export class StatusService {
#kv: KVNamespace;
#db: Database;

constructor(kv: KVNamespace) {
constructor(kv: KVNamespace, db: Database) {
this.#kv = kv;
this.#db = db;
}

async get() {
return Number(await this.#kv.get(STATUS_KEY));
async getOverride() {
const value = await this.#kv.get(OVERRIDE_KEY);
return value === null ? null : Number(value);
}

async get(now = new Date()) {
const override = await this.getOverride();
if (override !== null) return override;

// Compare Unix seconds, matching Drizzle's timestamp storage. Grouping keeps
// the bar open between shifts belonging to the same event, also past midnight.
const timestamp = Math.floor(now.getTime() / 1000);
const activeEvents = await this.#db
.select({ eventId: shifts.eventId })
.from(shifts)
.groupBy(shifts.eventId)
.having(sql`min(${shifts.startAt}) <= ${timestamp} and max(${shifts.endAt}) > ${timestamp}`)
.limit(1);
return activeEvents.length > 0 ? STATUS.OPEN : STATUS.CLOSED;
}

async setAutomatic() {
await this.#kv.delete(OVERRIDE_KEY);
}

async set(status: number) {
await this.#kv.put(STATUS_KEY, String(status));
await this.#kv.put(OVERRIDE_KEY, String(status));
}

async getWithMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fail } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals }) => {
const status = await locals.statusService.get();
const status = (await locals.statusService.getOverride()) ?? -1;

return {
status
Expand All @@ -13,15 +13,19 @@ export const actions: Actions = {
default: async ({ locals, request }) => {
const formData = await request.formData();
const status = Number(formData.get('status'));
const isValidStatus = [0, 1, 2].includes(status);
const isValidStatus = formData.has('status') && [-1, 0, 1, 2].includes(status);

if (!isValidStatus) {
return fail(400, {
message: 'Invalid status'
});
}

await locals.statusService.set(status);
if (status === -1) {
await locals.statusService.setAutomatic();
} else {
await locals.statusService.set(status);
}

return {
success: true
Expand Down
12 changes: 11 additions & 1 deletion programmerbar-web/src/routes/(portal)/portal/status/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Beer, Lock, Users, Loader } from '@lucide/svelte';
import { Beer, Lock, Users, Loader, Clock } from '@lucide/svelte';
import { cn } from '$lib/utils/cn.js';
import { enhance } from '$app/forms';

Expand All @@ -8,6 +8,13 @@
let newStatus = $state(data.status);

const statusButtons = [
{
status: -1,
Icon: Clock,
label: 'Automatisk',
description: '脜pent fra f酶rste vakt starter til siste vakt slutter i hvert arrangement',
color: 'green'
},
{
status: 1,
Icon: Beer,
Expand Down Expand Up @@ -40,6 +47,9 @@
<div class="text-center">
<h1 class="mb-2 text-2xl font-bold text-gray-800 dark:text-gray-100">Barstatus</h1>
<p class="text-gray-600 dark:text-gray-300">Velg gjeldende status for baren</p>
<p class="mt-2 text-sm text-gray-500 dark:text-gray-400">
Automatisk f酶lger vakttidene i portalen. Manuelle valg gjelder til du velger automatisk igjen.
</p>
</div>

<div class="space-y-3">
Expand Down
Loading