Compare commits

..

5 Commits

Author SHA1 Message Date
b08aca3732 upgrade deps 2026-06-15 14:54:06 +02:00
e58e589f8e WIP: auth public 2026-06-15 14:09:28 +02:00
794d81438d update deps 2026-01-27 17:47:04 +01:00
a17baba9c9 commit bruno updates 2026-01-27 17:43:56 +01:00
bcc02fbd78 add invoice unique constraints and rename tables 2026-01-13 09:49:03 +01:00
67 changed files with 2769 additions and 2127 deletions

View File

@ -0,0 +1,20 @@
meta {
name: /api/accounts/:number
type: http
seq: 3
}
get {
url: {{base_url}}/api/accounts/:number
body: none
auth: inherit
}
params:path {
number: 3011
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -0,0 +1,20 @@
meta {
name: /api/accounts/month-sum/:number
type: http
seq: 24
}
get {
url: {{base_url}}/api/accounts/month-sum/:number
body: none
auth: inherit
}
params:path {
number: 3011
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -0,0 +1,20 @@
meta {
name: /api/accounts/year-sum/:number
type: http
seq: 25
}
get {
url: {{base_url}}/api/accounts/year-sum/:number
body: none
auth: inherit
}
params:path {
number: 3011
}
settings {
encodeUrl: true
timeout: 0
}

View File

@ -1,7 +1,7 @@
meta {
name: /api/balances
type: http
seq: 3
seq: 4
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/entries/:id
type: http
seq: 5
seq: 6
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/entries
type: http
seq: 4
seq: 5
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/errors
type: http
seq: 22
seq: 23
}
delete {

View File

@ -1,7 +1,7 @@
meta {
name: /api/financial-years
type: http
seq: 6
seq: 7
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/invites
type: http
seq: 10
seq: 11
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/invoices/:id
type: http
seq: 8
seq: 9
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/invoices/total-amount
type: http
seq: 9
seq: 10
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/invoices
type: http
seq: 7
seq: 8
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/journals
type: http
seq: 21
seq: 22
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/objects/:id/transactions
type: http
seq: 12
seq: 13
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/objects
type: http
seq: 11
seq: 12
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/results/:year
type: http
seq: 14
seq: 15
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/results
type: http
seq: 13
seq: 14
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/roles
type: http
seq: 15
seq: 16
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/suppliers/:id
type: http
seq: 17
seq: 18
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/suppliers/merge
type: http
seq: 18
seq: 19
}
post {

View File

@ -1,7 +1,7 @@
meta {
name: /api/suppliers
type: http
seq: 16
seq: 17
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/transactions
type: http
seq: 19
seq: 20
}
get {

View File

@ -1,7 +1,7 @@
meta {
name: /api/users
type: http
seq: 20
seq: 21
}
get {

View File

@ -1,9 +1,9 @@
{
"*.{cjs,cts,js,jsx,mjs,mts,ts,tsx}": [
"oxlint --fix",
"prettier --write"
"oxfmt"
],
"*.{css,scss,json}": [
"prettier --write"
"oxfmt"
]
}

View File

@ -1,18 +0,0 @@
dist
dump
*.*
!*.cjs
!*.css
!*.cts
!*.html
!*.js
!*.json
!*.jsx
!*.mjs
!*.mts
!*.scss
!*.ts
!*.tsx
!*.yaml
!*.yml
pnpm-lock.yaml

View File

@ -1,17 +0,0 @@
{
"jsxSingleQuote": true,
"printWidth": 120,
"semi": false,
"singleQuote": true,
"trailingComma": "all",
"embeddedLanguageFormatting": "off",
"overrides": [
{
"files": "*.scss",
"options": {
"trailingComma": "none"
}
}
]
}

View File

@ -65,7 +65,7 @@ async function readdir(dir: string) {
}
const file = await trx.insertInto('file').values({ filename }).returning('id').executeTakeFirstOrThrow()
await trx.insertInto('filesToInvoice').values({ fileId: file.id, invoiceId: invoice.id }).execute()
await trx.insertInto('files_invoice').values({ fileId: file.id, invoiceId: invoice.id }).execute()
}
await trx.commit().execute()

View File

@ -68,7 +68,7 @@ for (const row of rows.toReversed()) {
}
const file = await trx.insertInto('file').values({ filename }).returning('id').executeTakeFirstOrThrow()
await trx.insertInto('filesToInvoice').values({ fileId: file.id, invoiceId: invoice.id }).execute()
await trx.insertInto('files_invoice').values({ fileId: file.id, invoiceId: invoice.id }).execute()
}
}

View File

@ -2,7 +2,7 @@ import { h, type FunctionComponent } from 'preact'
import { useState } from 'preact/hooks'
import { Router } from 'preact-router'
import CurrentUserContext from '../contexts/current_user.ts'
import CurrentUserContext from '../../shared/contexts/current_user.ts'
import { NotificationsProvider } from '../contexts/notifications.tsx'
import routes from '../routes.ts'
@ -22,7 +22,7 @@ const App: FunctionComponent<{ state: ANY }> = ({ state }) => {
<CurrentUserContext.Provider value={{ user, setUser }}>
<div className={s.base}>
<a href='/admin' className={s.logo}>
Carson Admin
BRF Admin
</a>
<header className={s.header}>

View File

@ -1,7 +1,7 @@
import { h, type FunctionComponent } from 'preact'
import cn from 'classnames'
import { useCurrentUser } from '../contexts/current_user.ts'
import { useCurrentUser } from '../../shared/contexts/current_user.ts'
import s from './current_user.module.scss'
const CurrentUser: FunctionComponent<{ className?: string }> = ({ className }) => {

View File

@ -1,7 +1,7 @@
import { h, type FunctionComponent, type TargetedSubmitEvent } from 'preact'
import type { FetchError } from 'rek'
import { useCallback } from 'preact/hooks'
import { useCurrentUser } from '../contexts/current_user.ts'
import { useCurrentUser } from '../../shared/contexts/current_user.ts'
import rek from 'rek'
import useRequestState from '../../shared/hooks/use_request_state.ts'

View File

@ -2,7 +2,7 @@
import { h } from 'preact'
import { useEffect, useState } from 'preact/hooks'
import { route } from 'preact-router'
import { useCurrentUser } from '../contexts/current_user.ts'
import { useCurrentUser } from '../../shared/contexts/current_user.ts'
/** @type {import('preact').FunctionComponent<{ auth: boolean, path: string, component: () => any, loadComponent: boolean}} Page */
const Route = ({ auth, path, component, loadComponent = true }) => {

View File

@ -3,6 +3,6 @@ import './styles/main.scss'
import { h, hydrate } from 'preact'
import App from './components/app.tsx'
const STATE = typeof __STATE__ !== 'undefined' ? __STATE__ : undefined
const state = typeof __STATE__ === 'undefined' ? { user: null } : __STATE__
hydrate(h(App, STATE), document.body)
hydrate(h(App, { state }), document.body)

View File

@ -0,0 +1,90 @@
import { h, type FunctionComponent } from 'preact'
import { useEffect, useRef } from 'preact/hooks'
import * as d3 from 'd3'
import rek from 'rek'
import Head from './head.ts'
import usePromise from '../../shared/hooks/use_promise.ts'
type AccountPageProps = {
number: number
}
function empty(element: HTMLElement) {
while (element.lastElementChild) {
element.removeChild(element.lastElementChild)
}
}
const AccountPage: FunctionComponent<AccountPageProps> = ({ number }) => {
const graphRef = useRef<HTMLDivElement>(null)
const sums = usePromise<{ month: string; totalAmount: number }[]>(() => rek(`/api/accounts/month-sum/${number}`))
useEffect(() => {
empty(graphRef.current!)
const width = 1000
const height = 1000
const margin = { left: 50, bottom: 40 }
const stageWidth = 1000 - margin.left
const stageHeight = 1000 - margin.bottom
const svg = d3
.select(graphRef.current)
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
const x = d3.scaleBand(
sums.map(({ month }) => month),
[0, stageWidth],
)
svg.append('g').attr('transform', `translate(0, ${stageHeight})`).call(d3.axisBottom(x))
const y = d3.scaleLinear([0, d3.max(sums, (d) => Math.abs(d.totalAmount))], [stageHeight, 0])
svg.append('g').call(d3.axisLeft(y))
svg
.append('path')
.datum(sums)
.attr('fill', 'none')
.attr('stroke', 'steelblue')
.attr('stroke-width', 1.5)
.attr(
'd',
d3
.line()
.x((d) => x(d.month))
.y((d) => y(Math.abs(d.totalAmount))),
)
}, [])
return (
<section>
<Head>
<title> : Konto</title>
</Head>
<h1>Konto {number}</h1>
<div ref={graphRef} />
<table className='grid'>
<thead>
<th>Månad</th>
<th>Summa</th>
</thead>
<tbody>
{sums.map((sum) => (
<tr>
<td>{sum.month}</td>
<td>{sum.totalAmount}</td>
</tr>
))}
</tbody>
</table>
</section>
)
}
export default AccountPage

View File

@ -23,7 +23,9 @@ const AccountsPage: FunctionComponent = () => {
<tbody>
{accounts.map((account) => (
<tr>
<td>{account.number}</td>
<td>
<a href={`/accounts/${account.number}`}>{account.number}</a>
</td>
<td>{account.description}</td>
</tr>
))}

View File

@ -1,19 +1,22 @@
import { h, type FunctionComponent } from 'preact'
import { useCallback, useEffect, useRef } from 'preact/hooks'
import { LocationProvider, Route, Router } from 'preact-iso'
import { LocationProvider, Router } from 'preact-iso'
import { get } from 'lowline'
import { AuthProvider } from '../../shared/contexts/auth.tsx'
import throttle from '../../shared/utils/throttle.ts'
import Head from './head.ts'
import Footer from './footer.tsx'
import Header from './header.tsx'
import ErrorPage from './error_page.tsx'
import { AuthRoute, Refresh } from './route.tsx'
import routes from '../routes.ts'
import throttle from '../../shared/utils/throttle.ts'
import s from './app.module.scss'
type Props = {
error?: Error
title?: string
state: ANY
}
const scroll = () => {
@ -22,7 +25,7 @@ const scroll = () => {
window.scrollTo(0, offset || 0)
}
const App: FunctionComponent<Props> = ({ error, title }) => {
const App: FunctionComponent<Props> = ({ error, title, state }) => {
const loadRef = useRef<boolean>(false)
useEffect(() => {
@ -61,9 +64,10 @@ const App: FunctionComponent<Props> = ({ error, title }) => {
return (
<LocationProvider>
<AuthProvider user={state.user}>
<div id='app' className={s.base}>
<Head>
<title>{title || 'Untitled'}</title>
<title>{title || 'BRF'}</title>
</Head>
<Header routes={routes} />
@ -74,14 +78,16 @@ const App: FunctionComponent<Props> = ({ error, title }) => {
) : (
<Router onLoadStart={onLoadStart} onLoadEnd={onLoadEnd} onRouteChange={onRouteChange}>
{routes.map((route) => (
<Route key={route.path} path={route.path} component={route.component} />
<AuthRoute key={route.path} {...route} />
))}
<Refresh path='/admin' />
</Router>
)}
</main>
<Footer />
</div>
</AuthProvider>
</LocationProvider>
)
}

View File

@ -0,0 +1,5 @@
.links {
display: flex;
gap: 10px;
justify-content: end;
}

View File

@ -0,0 +1,38 @@
import { h, type FunctionComponent } from 'preact'
import { Show } from '@preact/signals/utils'
import { useComputed } from '@preact/signals'
import cn from 'classnames'
import { useAuth } from '../../shared/contexts/auth.tsx'
import s from './current_user.module.scss'
const CurrentUser: FunctionComponent<{ className?: string }> = ({ className }) => {
const { user } = useAuth()
const noUser = useComputed(() => !user.value)
return (
<div className={cn(s.base, className)}>
<Show when={user}>
<div className={s.email}>{user.value?.email}</div>
<div className={s.links}>
<a href='/auth/logout' target='_parent'>
Logout
</a>
</div>
</Show>
<Show when={noUser}>
<div className={cn(s.base, className)}>
<p>You are not logged in</p>
<div className={s.links}>
<a href='/login'>Login</a>
<a href='/register'>Register</a>
</div>
</div>
</Show>
</div>
)
}
export default CurrentUser

View File

@ -1,6 +1,14 @@
@use '../../shared/styles/utils';
.base {
display: grid;
grid-template-columns: 1fr max-content;
grid-template-rows: auto auto;
}
.nav {
grid-area: 2 / 1 / 3 / 2;
> ul {
@include utils.wipe-list();
@ -11,6 +19,16 @@
display: block;
padding: 5px;
}
&:first-child {
> a {
padding-left: 0;
}
}
}
}
}
.currentUser {
grid-area: 1 / 2 / 3 / 3;
}

View File

@ -1,10 +1,11 @@
import { h, type FunctionComponent } from 'preact'
import CurrentUser from './current_user.tsx'
import s from './header.module.scss'
import type { Route } from '../../../shared/types.ts'
const Header: FunctionComponent<{ routes: Route[] }> = ({ routes }) => (
<header>
<header className={s.base}>
<h1>BRF Tegeltrasten</h1>
<nav className={s.nav}>
<ul>
@ -17,6 +18,7 @@ const Header: FunctionComponent<{ routes: Route[] }> = ({ routes }) => (
)}
</ul>
</nav>
<CurrentUser className={s.currentUser} />
</header>
)

View File

@ -0,0 +1,69 @@
@use 'sass:color';
.base {
display: flex;
flex-direction: column;
&:not(:last-child):not(.noMargin) {
margin-bottom: var(--gutter);
}
}
.label {
order: 1;
font-weight: bold;
margin-bottom: var(--form-label-margin);
&:has(input:invalid) {
color: var(--form-invalid-color);
}
&:has(input:valid) {
color: var(--form-valid-color);
}
}
.element {
order: 2;
width: 100%;
box-shadow: none;
border: 1px solid var(--form-border-color);
padding: var(--form-element-padding);
font-family: var(--body-font-family);
background: white;
&:focus {
outline: none;
border-color: var(--color-blue);
// box-shadow: 0 0 5px 0px color.scale(var(--color-blue), $alpha: -20%);
}
.touched &:invalid {
border-color: var(--form-invalid-color);
~ .label {
color: var(--form-invalid-color);
}
&:focus {
// box-shadow: 0 0 5px 0px color.scale(var(--form-invalid-color), $alpha: -20%);
}
}
.touched &:valid {
border-color: var(--form-valid-color);
~ .label {
color: var(--form-valid-color);
}
&:focus {
// box-shadow: 0 0 5px 0px color.scale(var(--form-valid-color), $alpha: -20%);
}
}
}
.errorLabel {
color: var(--form-invalid-color);
margin-top: var(--form-label-margin);
}

View File

@ -0,0 +1,5 @@
import inputFactory from '../../shared/components/input_factory.tsx'
import s from './input.module.scss'
export default inputFactory(s)

View File

@ -0,0 +1,30 @@
.root {
max-width: 400px;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto;
}
.error,
.input {
grid-column: 1 / 3;
}
.footer {
display: flex;
justify-content: space-between;
align-items: center;
}
.button {
grid-column: 2 / 3;
}
.links {
grid-column: 1 / 3;
justify-self: center;
li:not(:last-child) {
padding-bottom: 4px;
}
}

View File

@ -0,0 +1,77 @@
import { h, type FunctionComponent, type TargetedSubmitEvent } from 'preact'
import type { FetchError } from 'rek'
import { useCallback } from 'preact/hooks'
import { useAuth } from '../../shared/contexts/auth.tsx'
import rek from 'rek'
import useRequestState from '../../shared/hooks/use_request_state.ts'
import Input from './input.tsx'
import s from './login_form.module.scss'
const LoginForm: FunctionComponent = () => {
const [{ error, pending }, actions] = useRequestState<FetchError>()
const { user } = useAuth()
const login = useCallback(async (e: TargetedSubmitEvent<HTMLFormElement>) => {
e.preventDefault()
actions.pending()
const form = e.currentTarget
try {
const response = await rek.post(
'/auth/login',
{
email: form.email.value,
password: form.password.value,
},
{ response: false },
)
const lastVisit = response.headers.get('x-last-visit')
if (lastVisit) {
localStorage.setItem('lastVisit', lastVisit)
}
const result = await response.json()
user.value = result
} catch (err) {
actions.error(err as FetchError)
}
}, [])
return (
<form onSubmit={login} className={s.root}>
{error && <div className={s.error}>Error: {error.body?.message || error.message}</div>}
<Input className={s.input} name='email' label='Email' placeholder='Email' type='email' autoFocus required />
<Input
className={s.input}
type='password'
name='password'
label='Password'
placeholder='Password'
autoComplete='current-password'
required
/>
<button className={s.button} type='submit' disabled={pending}>
Login
</button>
<ul className={s.links}>
<li>
<a href='/admin/forgot-password'>Forgot your password?</a>
</li>
<li>
<a href='/admin/register'>No account? Register a new</a>
</li>
</ul>
</form>
)
}
export default LoginForm

View File

@ -0,0 +1,10 @@
import { h } from 'preact'
import LoginForm from './login_form.tsx'
const LoginPage = () => (
<section>
<LoginForm />
</section>
)
export default LoginPage

View File

@ -0,0 +1,34 @@
import { h, type FunctionComponent } from 'preact'
import { useLocation, type RouteProps } from 'preact-iso'
import { useAuth } from '../../shared/contexts/auth.tsx'
type AuthRouteProps = {
auth?: boolean
user?: any
} & RouteProps<any>
export const Refresh: FunctionComponent<{ path: string }> = () => {
location.replace(location.href)
}
export const Redirect: FunctionComponent<{ to: string; replace: boolean }> = ({ to, replace = false }) => {
const { route } = useLocation()
route(to, replace)
}
export const AuthRoute: FunctionComponent<AuthRouteProps> = ({ auth, component, ...props }) => {
const { user } = useAuth()
if (auth === true && !user.value) {
return <Redirect to='/login' replace={true} />
} else if (auth === false && user.value) {
const url = localStorage.getItem('lastVisit') || '/'
localStorage.removeItem('lastVisit')
return <Redirect to={url} replace={true} />
} else {
return h(component, props)
}
}

View File

@ -71,7 +71,9 @@ const TransactionsPage: FunctionComponent = () => {
{transactions.map((transaction) => (
<tr>
<td>{(transaction.transactionDate as unknown as string)?.slice(0, 10)}</td>
<td>{transaction.accountNumber}</td>
<td>
<a href={`/accounts/${transaction.accountNumber}`}>{transaction.accountNumber}</a>
</td>
<td className='tar'>
{(transaction.amount as unknown as number) >= 0
? formatNumber(transaction.amount as unknown as number)

View File

@ -1,3 +1,4 @@
import Account from './components/account_page.tsx'
import Accounts from './components/accounts_page.tsx'
import Balances from './components/balances_page.tsx'
import Entries from './components/entries_page.tsx'
@ -5,6 +6,7 @@ import Entry from './components/entry_page.tsx'
import Invoice from './components/invoice_page.tsx'
import Invoices from './components/invoices_page.tsx'
import InvoicesBySupplier from './components/invoices_by_supplier_page.tsx'
import Login from './components/login_page.tsx'
import Objects from './components/objects_page.tsx'
import Results from './components/results_page.tsx'
import Start from './components/start_page.tsx'
@ -16,18 +18,29 @@ export default [
name: 'start',
title: 'Start',
component: Start,
auth: true,
},
{
path: '/accounts',
name: 'accounts',
title: 'Konton',
component: Accounts,
auth: true,
},
{
path: '/accounts/:number',
name: 'account',
title: 'Konto',
component: Account,
nav: false,
auth: true,
},
{
path: '/entries',
name: 'entries',
title: 'Verifikat',
component: Entries,
auth: true,
},
{
path: '/entries/:id',
@ -35,18 +48,21 @@ export default [
title: 'Verifikat :id',
component: Entry,
nav: false,
auth: true,
},
{
path: '/objects',
name: 'objects',
title: 'Objekt',
component: Objects,
auth: true,
},
{
path: 'invoices',
name: 'invoices',
title: 'Fakturor',
component: Invoices,
auth: true,
},
{
path: '/invoices/:id',
@ -54,6 +70,7 @@ export default [
title: 'Faktura :id',
component: Invoice,
nav: false,
auth: true,
},
{
path: '/invoices/by-supplier/:supplier',
@ -61,23 +78,35 @@ export default [
title: 'Fakturor från leverantör',
component: InvoicesBySupplier,
nav: false,
auth: true,
},
{
path: '/balances',
name: 'balances',
title: 'Balanser',
component: Balances,
auth: true,
},
{
path: '/results',
name: 'results',
title: 'Resultat',
component: Results,
auth: true,
},
{
path: '/transactions',
name: 'transactions',
title: 'Transaktioner',
component: Transactions,
auth: true,
},
{
path: '/login',
name: 'login',
title: 'Logga in',
component: Login,
nav: false,
auth: false,
},
]

View File

@ -1,3 +1,14 @@
:root {
--color-green: green;
--color-red: red;
--gutter: 16px;
--form-valid-color: var(--color-green);
--form-invalid-color: var(--color-red);
--form-border-color: #d2d6de;
--form-element-padding: 9px;
--form-label-margin: 6px;
}
*,
*:before,
*:after {

View File

@ -0,0 +1,15 @@
import { h, createContext, type FunctionComponent } from 'preact'
import { useSignal } from '@preact/signals'
import { useContext } from 'preact/hooks'
type AuthContextType = { user: ANY }
const AuthContext = createContext<AuthContextType | null>(null)
export const AuthProvider: FunctionComponent<{ user: ANY }> = ({ children, user }) => {
const userSignal = useSignal(user)
return <AuthContext.Provider value={{ user: userSignal }}>{children}</AuthContext.Provider>
}
export const useAuth = () => useContext(AuthContext) as AuthContextType

View File

@ -2,7 +2,7 @@
-- PostgreSQL database dump
--
\restrict kNYhdwOhwE9I3bgAzdljyYgB5xyEpjhiaSCeYZfp84v3ey1GpvsdxX4U8Y8fQM3
\restrict wU1dfnHqRTt2y70XvbaA1LDda0PgjnckSPP98WqYNl91ofZctLJ9wjIOEE8bpwR
-- Dumped from database version 18.1
-- Dumped by pg_dump version 18.1
@ -71,10 +71,10 @@ ALTER SEQUENCE public.account_id_seq OWNED BY public.account.id;
--
-- Name: aliasesToSupplier; Type: TABLE; Schema: public; Owner: -
-- Name: aliases_supplier; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public."aliasesToSupplier" (
CREATE TABLE public.aliases_supplier (
id integer NOT NULL,
"supplierId" integer NOT NULL,
alias text NOT NULL
@ -82,10 +82,10 @@ CREATE TABLE public."aliasesToSupplier" (
--
-- Name: aliasesToSupplier_id_seq; Type: SEQUENCE; Schema: public; Owner: -
-- Name: aliases_supplier_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public."aliasesToSupplier_id_seq"
CREATE SEQUENCE public.aliases_supplier_id_seq
AS integer
START WITH 1
INCREMENT BY 1
@ -95,10 +95,10 @@ CREATE SEQUENCE public."aliasesToSupplier_id_seq"
--
-- Name: aliasesToSupplier_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
-- Name: aliases_supplier_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public."aliasesToSupplier_id_seq" OWNED BY public."aliasesToSupplier".id;
ALTER SEQUENCE public.aliases_supplier_id_seq OWNED BY public.aliases_supplier.id;
--
@ -199,10 +199,10 @@ ALTER SEQUENCE public.file_id_seq OWNED BY public.file.id;
--
-- Name: filesToInvoice; Type: TABLE; Schema: public; Owner: -
-- Name: files_invoice; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public."filesToInvoice" (
CREATE TABLE public.files_invoice (
"invoiceId" integer NOT NULL,
"fileId" integer NOT NULL
);
@ -442,10 +442,10 @@ ALTER SEQUENCE public.transaction_id_seq OWNED BY public.transaction.id;
--
-- Name: transactionsToObjects; Type: TABLE; Schema: public; Owner: -
-- Name: transactions_objects; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public."transactionsToObjects" (
CREATE TABLE public.transactions_objects (
"transactionId" integer NOT NULL,
"objectId" integer NOT NULL
);
@ -459,10 +459,10 @@ ALTER TABLE ONLY public.account ALTER COLUMN id SET DEFAULT nextval('public.acco
--
-- Name: aliasesToSupplier id; Type: DEFAULT; Schema: public; Owner: -
-- Name: aliases_supplier id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."aliasesToSupplier" ALTER COLUMN id SET DEFAULT nextval('public."aliasesToSupplier_id_seq"'::regclass);
ALTER TABLE ONLY public.aliases_supplier ALTER COLUMN id SET DEFAULT nextval('public.aliases_supplier_id_seq'::regclass);
--
@ -560,19 +560,19 @@ ALTER TABLE ONLY public.account
--
-- Name: aliasesToSupplier aliasesToSupplier_alias_key; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: aliases_supplier aliases_supplier_alias_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."aliasesToSupplier"
ADD CONSTRAINT "aliasesToSupplier_alias_key" UNIQUE (alias);
ALTER TABLE ONLY public.aliases_supplier
ADD CONSTRAINT aliases_supplier_alias_key UNIQUE (alias);
--
-- Name: aliasesToSupplier aliasesToSupplier_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: aliases_supplier aliases_supplier_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."aliasesToSupplier"
ADD CONSTRAINT "aliasesToSupplier_pkey" PRIMARY KEY (id);
ALTER TABLE ONLY public.aliases_supplier
ADD CONSTRAINT aliases_supplier_pkey PRIMARY KEY (id);
--
@ -600,11 +600,11 @@ ALTER TABLE ONLY public.file
--
-- Name: filesToInvoice filesToInvoice_pkey; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: files_invoice files_invoice_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."filesToInvoice"
ADD CONSTRAINT "filesToInvoice_pkey" PRIMARY KEY ("invoiceId", "fileId");
ALTER TABLE ONLY public.files_invoice
ADD CONSTRAINT files_invoice_pkey PRIMARY KEY ("invoiceId", "fileId");
--
@ -631,6 +631,22 @@ ALTER TABLE ONLY public."financialYear"
ADD CONSTRAINT "financialYear_year_key" UNIQUE (year);
--
-- Name: invoice invoice_fiskenNumber_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.invoice
ADD CONSTRAINT "invoice_fiskenNumber_key" UNIQUE ("fiskenNumber");
--
-- Name: invoice invoice_phmNumber_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.invoice
ADD CONSTRAINT "invoice_phmNumber_key" UNIQUE ("phmNumber");
--
-- Name: invoice invoice_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -696,35 +712,35 @@ ALTER TABLE ONLY public.transaction
--
-- Name: transactionsToObjects transactionsToObjects_transactionId_objectId_key; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: transactions_objects transactions_objects_transactionId_objectId_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."transactionsToObjects"
ADD CONSTRAINT "transactionsToObjects_transactionId_objectId_key" UNIQUE ("transactionId", "objectId");
ALTER TABLE ONLY public.transactions_objects
ADD CONSTRAINT "transactions_objects_transactionId_objectId_key" UNIQUE ("transactionId", "objectId");
--
-- Name: aliasesToSupplier aliasesToSupplier_supplierId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: aliases_supplier aliases_supplier_supplierId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."aliasesToSupplier"
ADD CONSTRAINT "aliasesToSupplier_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES public.supplier(id);
ALTER TABLE ONLY public.aliases_supplier
ADD CONSTRAINT "aliases_supplier_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES public.supplier(id);
--
-- Name: filesToInvoice filesToInvoice_fileId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: files_invoice files_invoice_fileId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."filesToInvoice"
ADD CONSTRAINT "filesToInvoice_fileId_fkey" FOREIGN KEY ("fileId") REFERENCES public.file(id);
ALTER TABLE ONLY public.files_invoice
ADD CONSTRAINT "files_invoice_fileId_fkey" FOREIGN KEY ("fileId") REFERENCES public.file(id);
--
-- Name: filesToInvoice filesToInvoice_invoiceId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: files_invoice files_invoice_invoiceId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."filesToInvoice"
ADD CONSTRAINT "filesToInvoice_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES public.invoice(id);
ALTER TABLE ONLY public.files_invoice
ADD CONSTRAINT "files_invoice_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES public.invoice(id);
--
@ -784,24 +800,24 @@ ALTER TABLE ONLY public.transaction
--
-- Name: transactionsToObjects transactionsToObjects_objectId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: transactions_objects transactions_objects_objectId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."transactionsToObjects"
ADD CONSTRAINT "transactionsToObjects_objectId_fkey" FOREIGN KEY ("objectId") REFERENCES public.object(id);
ALTER TABLE ONLY public.transactions_objects
ADD CONSTRAINT "transactions_objects_objectId_fkey" FOREIGN KEY ("objectId") REFERENCES public.object(id);
--
-- Name: transactionsToObjects transactionsToObjects_transactionId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- Name: transactions_objects transactions_objects_transactionId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public."transactionsToObjects"
ADD CONSTRAINT "transactionsToObjects_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES public.transaction(id);
ALTER TABLE ONLY public.transactions_objects
ADD CONSTRAINT "transactions_objects_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES public.transaction(id);
--
-- PostgreSQL database dump complete
--
\unrestrict kNYhdwOhwE9I3bgAzdljyYgB5xyEpjhiaSCeYZfp84v3ey1GpvsdxX4U8Y8fQM3
\unrestrict wU1dfnHqRTt2y70XvbaA1LDda0PgjnckSPP98WqYNl91ofZctLJ9wjIOEE8bpwR

View File

@ -20,11 +20,11 @@ auth_tables = [
accounting_tables = [
'account',
'accountBalance',
'aliasesToSupplier',
'aliases_supplier',
'dimension',
'entry',
'file',
'filesToInvoice',
'files_invoice',
'financialYear',
'invoice',
'journal',
@ -32,7 +32,7 @@ accounting_tables = [
'supplier',
'supplierType',
'transaction',
'transactionsToObjects',
'transactions_objects',
]
parser = argparse.ArgumentParser()

View File

@ -12,7 +12,8 @@
"build:client": "VITE_ENTRY=public vite build && VITE_ENTRY=admin vite build",
"build:server": "vite build --ssr",
"clean": "rm -r dist",
"format": "prettier --write .",
"fmt": "oxfmt",
"fmt:check": "oxfmt --check",
"lint": "oxlint",
"prepare": "husky",
"start": "node server/index.ts",
@ -28,49 +29,48 @@
"@bmp/highlight-stack": "^0.1.2",
"@domp/suppress": "^0.4.0",
"@fastify/cookie": "^11.0.2",
"@fastify/middie": "^9.0.3",
"@fastify/middie": "^9.3.2",
"@fastify/session": "^11.1.1",
"@fastify/static": "^8.3.0",
"@fastify/static": "^9.1.3",
"@fastify/type-provider-typebox": "^6.1.0",
"@preact/signals": "^2.9.1",
"chalk": "^5.6.2",
"classnames": "^2.5.1",
"d3": "^7.9.0",
"easy-tz": "^0.2.0",
"fastify": "^5.6.2",
"fastify-plugin": "^5.1.0",
"fastify": "^5.8.5",
"fastify-plugin": "^6.0.0",
"fastify-session-redis-store": "^7.1.2",
"fastify-type-provider-zod": "^6.1.0",
"ioredis": "^5.8.2",
"kysely": "^0.28.9",
"lodash": "^4.17.21",
"ioredis": "^5.11.1",
"kysely": "^0.29.2",
"lodash": "^4.18.1",
"lowline": "^0.4.2",
"mini-qs": "^0.2.0",
"pg": "^8.16.3",
"pg-protocol": "^1.10.3",
"pg": "^8.21.0",
"pg-protocol": "^1.14.0",
"pino-abstract-transport": "^3.0.0",
"preact": "^10.28.0",
"preact-iso": "^2.11.0",
"preact": "^10.29.2",
"preact-iso": "^2.12.0",
"preact-router": "^4.1.2",
"rek": "^0.8.1",
"zod": "^4.2.1"
"zod": "^4.4.3"
},
"devDependencies": {
"@babel/core": "^7.26.10",
"@preact/preset-vite": "^2.10.1",
"@preact/preset-vite": "^2.10.5",
"@testing-library/preact": "^3.2.4",
"@types/d3-dsv": "^3.0.7",
"@types/lodash": "^4.17.16",
"@types/node": "^24.10.1",
"@typescript/native-preview": "7.0.0-dev.20251126.1",
"@types/lodash": "^4.17.24",
"@types/node": "^25.9.3",
"@typescript/native-preview": "7.0.0-dev.20260614.1",
"d3-dsv": "^3.0.1",
"esbuild": "^0.27.0",
"globals": "^16.0.0",
"husky": "^9.1.7",
"jsdom": "^27.2.0",
"lint-staged": "^16.2.7",
"oxlint": "^1.29.0",
"prettier": "^3.5.3",
"sass": "^1.85.1",
"typebox": "^1.0.55",
"vite": "^7.2.4"
"jsdom": "^29.1.1",
"lint-staged": "^17.0.7",
"oxfmt": "^0.54.0",
"oxlint": "^1.69.0",
"sass": "^1.101.0",
"typebox": "^1.2.10",
"vite": "^8.0.16"
}
}

3894
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -26,9 +26,9 @@ const maxLoginAttempts = 5
const requireVerification = true
const redirects = {
login: '/admin/',
logout: '/admin/login',
register: '/admin/login',
login: '/',
logout: '/login',
register: '/login',
}
const remember = {

View File

@ -321,7 +321,7 @@ export default async function parseStream(stream: ReadableStream, decoder: Decod
}
await trx
.insertInto('transactionsToObjects')
.insertInto('transactions_objects')
.values({
transactionId,
objectId,

View File

@ -1,6 +1,6 @@
import build from 'pino-abstract-transport'
export default function dbTransport(create) {
export default function dbTransport(create: ANY) {
return build(async (source) => {
for await (const obj of source) {
// TODO decide how to handle lower log levels

View File

@ -74,6 +74,13 @@ const login: RouteHandler<{ Body: z.infer<typeof BodySchema> }> = async function
.where('id', '=', user.id)
.execute()
const lastVisit = request.session.get('lastVisit')
if (lastVisit) {
reply.header('x-last-visit', lastVisit)
request.session.set('lastVisit', undefined)
}
return reply.status(200).send(_.omit(user, 'password'))
} catch (err) {
this.log.error(err)

View File

@ -9,7 +9,7 @@ const schema: FastifySchema = {
const logout: RouteHandler = async function (request, reply) {
await request.logout()
return reply.redirect('/admin/login')
return reply.redirect('/login')
}
export default {

View File

@ -3,6 +3,7 @@ import * as z from 'zod'
import type { FastifyPluginCallbackZod } from 'fastify-type-provider-zod'
import { AccountSchema } from '../../schemas/db.ts'
import { sql } from 'kysely'
const accountRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
const { db } = fastify
@ -20,6 +21,88 @@ const accountRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
},
})
fastify.route({
url: '/:number',
method: 'GET',
schema: {
params: z.object({
number: z.coerce.number(),
}),
// response: {
// 200: z.array(AccountSchema),
// },
},
handler(request) {
return db
.selectFrom('account as a')
.innerJoin('transaction as t', 't.accountNumber', 'a.number')
.innerJoin('entry as e', 'e.id', 't.entryId')
.selectAll('a')
.select('e.transactionDate')
.where('a.number', '=', request.params.number)
.orderBy('e.transactionDate', 'asc')
.execute()
},
})
fastify.route({
url: '/month-sum/:number',
method: 'GET',
schema: {
params: z.object({
number: z.coerce.number(),
}),
response: {
200: z.array(
z.object({
month: z.string(),
totalAmount: z.coerce.number(),
}),
),
},
},
handler(request) {
return db
.selectFrom('transaction as t')
.innerJoin('entry as e', 'e.id', 't.entryId')
.select([
sql<string>`to_char(date_trunc('month', e."transactionDate"), 'YYYY-MM')`.as('month'),
sql<number>`sum(amount)`.as('totalAmount'),
])
.groupBy(sql`date_trunc('month', e."transactionDate")`)
.orderBy('month')
.where('t.accountNumber', '=', request.params.number)
.execute()
},
})
fastify.route({
url: '/year-sum/:number',
method: 'GET',
schema: {
params: z.object({
number: z.coerce.number(),
}),
},
handler(request) {
return (
db
.selectFrom('transaction as t')
.innerJoin('entry as e', 'e.id', 't.entryId')
// .select([sql`date_trunc('year', e."transactionDate")`.as('year'), sql<number>`sum(amount)`.as('totalAmount')])
.select([
sql`to_char(date_trunc('year', e."transactionDate"), 'YYYY')`.as('year'),
sql<number>`sum(amount)`.as('totalAmount'),
])
.groupBy(sql`date_trunc('year', e."transactionDate")`)
.orderBy('year')
.where('t.accountNumber', '=', request.params.number)
.execute()
)
},
})
done()
}

View File

@ -31,7 +31,7 @@ const invoiceRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
jsonArrayFrom(
eb
.selectFrom('file as f')
.innerJoin('filesToInvoice as fi', 'f.id', 'fi.fileId')
.innerJoin('files_invoice as fi', 'f.id', 'fi.fileId')
.select(['id', 'filename'])
.whereRef('fi.invoiceId', '=', 'i.id'),
).as('files'),
@ -89,7 +89,7 @@ const invoiceRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
jsonArrayFrom(
eb
.selectFrom('file as f')
.innerJoin('filesToInvoice as fi', 'f.id', 'fi.fileId')
.innerJoin('files_invoice as fi', 'f.id', 'fi.fileId')
.select(['id', 'filename'])
.whereRef('fi.invoiceId', '=', 'i.id'),
).as('files'),

View File

@ -28,7 +28,7 @@ const objectRoutes: FastifyPluginCallbackZod = (fastify, _, done) => {
async handler(request) {
return db
.selectFrom('transaction as t')
.innerJoin('transactionsToObjects as to', 't.id', 'to.transactionId')
.innerJoin('transactions_objects as to', 't.id', 'to.transactionId')
.innerJoin('entry as e', 'e.id', 't.entryId')
.select(['t.entryId', 'e.transactionDate', 't.accountNumber', 't.amount'])
.where('to.objectId', '=', request.params.id)

View File

@ -48,6 +48,19 @@ export default async (options?: FastifyServerOptions) => {
server.register(vitePlugin, {
mode: env.NODE_ENV,
createErrorHandler: ErrorHandler,
createPreHandler(route) {
return async function preHandler(request, reply) {
if (request.session.userId) {
return (reply.ctx = { user: await request.user })
} else if (route.auth) {
if (request.url !== '/') {
request.session.set('lastVisit', request.url)
}
return reply.redirect('/login')
}
}
},
entries: {
public: {
path: '/',
@ -56,15 +69,6 @@ export default async (options?: FastifyServerOptions) => {
admin: {
path: '/admin',
template: templateAdmin,
createPreHandler(route) {
return async function preHandler(request, reply) {
if (request.session.userId) {
return (reply.ctx = { user: await request.user })
} else if (route.auth) {
return reply.redirect('/admin/login')
}
}
},
},
},
})

View File

@ -16,7 +16,7 @@ interface Options {
}
export default ({ content, css, head, preload, script, state }: Options) => html`<!DOCTYPE html>
<html lang='sv-SE'>
<html lang="sv-SE">
<head>
<link rel="icon" href="/favicon.svg" />
<script type="module" src="${script}"></script>
@ -38,6 +38,9 @@ export default ({ content, css, head, preload, script, state }: Options) => html
<body>
${content}
${state?.then((state) => `<script>window.__STATE__ = ${JSON.stringify(state)}</script>`)}
<script>var offset=window.history.state&&window.history.state.scrollTop||0;if(offset)window.scrollTo(0,offset)</script>
<script>
var offset = (window.history.state && window.history.state.scrollTop) || 0;
if (offset) window.scrollTo(0, offset);
</script>
</body>
</html>`

1
shared/global.d.ts vendored
View File

@ -13,6 +13,7 @@ declare global {
declare module 'fastify' {
interface Session {
lastVisit?: string
userId: number
}

View File

@ -55,7 +55,7 @@ export interface AdmissionsRoles {
roleId: number
}
export interface AliasesToSupplier {
export interface AliasesSupplier {
alias: string
id: Generated<number>
supplierId: number
@ -108,7 +108,7 @@ export interface File {
id: Generated<number>
}
export interface FilesToInvoice {
export interface FilesInvoice {
fileId: number
invoiceId: number
}
@ -206,7 +206,7 @@ export interface Transaction {
transactionDate: Timestamp | null
}
export interface TransactionsToObjects {
export interface TransactionsObjects {
objectId: number
transactionId: number
}
@ -237,13 +237,13 @@ export interface DB {
accountBalance: AccountBalance
admission: Admission
admissions_roles: AdmissionsRoles
aliasesToSupplier: AliasesToSupplier
aliases_Supplier: AliasesSupplier
dimension: Dimension
emailToken: EmailToken
entry: Entry
error: Error
file: File
filesToInvoice: FilesToInvoice
files_invoice: FilesInvoice
financialYear: FinancialYear
invite: Invite
invites_roles: InvitesRoles
@ -255,7 +255,7 @@ export interface DB {
supplier: Supplier
supplierType: SupplierType
transaction: Transaction
transactionsToObjects: TransactionsToObjects
transactions_objects: TransactionsObjects
user: User
users_roles: UsersRoles
}