-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.yaml
More file actions
891 lines (855 loc) · 26.1 KB
/
api.yaml
File metadata and controls
891 lines (855 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
openapi: 3.0.3
info:
title: GatherYourDeals Data API
description: |
API for managing purchase data and user authentication.
## Authentication
This API uses JWT Bearer tokens.
1. Register a user via `POST /users`
2. Log in via `POST /auth/login` — returns an access token and a refresh token
3. Include the access token in the `Authorization: Bearer <token>` header for protected requests
4. When the access token expires, call `POST /auth/refresh` with the refresh token to get a new pair
5. Log out via `POST /auth/logout` — revokes the refresh token
version: 0.4.0
license:
name: MIT
servers:
- url: http://localhost:8080/api/v1
description: Local development server
tags:
- name: Users
description: User registration
- name: Sessions
description: Login, logout, token refresh, and current user info
- name: Meta
description: Field metadata — list all registered fields
- name: Receipts
description: Purchase records — create, list, get, and delete
- name: Admin - Users
description: User management (admin only)
- name: Admin - Meta
description: Field metadata management (admin only)
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
description: JWT access token
parameters:
offsetParam:
name: offset
in: query
required: false
schema:
type: integer
minimum: 0
default: 0
description: Number of records to skip
example: 0
limitParam:
name: limit
in: query
required: false
schema:
type: integer
minimum: 1
maximum: 100
default: 20
description: Maximum number of records to return. Values above 100 are silently capped to 100.
example: 20
schemas:
Error:
type: object
properties:
error:
type: string
example:
error: "invalid username or password"
UserPage:
type: object
properties:
data:
type: array
items:
$ref: "#/components/schemas/User"
total:
type: integer
description: Total number of matching records (ignoring pagination)
example: 42
offset:
type: integer
example: 0
limit:
type: integer
example: 20
total_pages:
type: integer
description: "Ceiling division: ceil(total / limit). 0 when total is 0."
example: 3
ReceiptPage:
type: object
properties:
data:
type: array
items:
$ref: "#/components/schemas/Receipt"
total:
type: integer
example: 42
offset:
type: integer
example: 0
limit:
type: integer
example: 20
total_pages:
type: integer
example: 3
MetaFieldPage:
type: object
properties:
data:
type: array
items:
$ref: "#/components/schemas/MetaField"
total:
type: integer
example: 8
offset:
type: integer
example: 0
limit:
type: integer
example: 20
total_pages:
type: integer
example: 1
User:
type: object
properties:
id:
type: string
format: uuid
example: "550e8400-e29b-41d4-a716-446655440000"
username:
type: string
example: "alice"
role:
type: string
enum: [admin, user]
example: "user"
TokenResponse:
type: object
properties:
access_token:
type: string
description: Short-lived JWT for authenticating requests
example: "eyJhbGciOiJIUzI1NiIs..."
refresh_token:
type: string
description: Long-lived token used to obtain a new access token
example: "eyJhbGciOiJIUzI1NiIs..."
token_type:
type: string
description: |
Always "Bearer". Defined by RFC 6750 — the client must include this
token in the Authorization header as: Authorization: Bearer <access_token>
example: "Bearer"
MetaField:
type: object
properties:
fieldName:
type: string
example: "brand"
description:
type: string
example: "brand of the product"
type:
type: string
example: "string"
native:
type: boolean
description: Whether this is a built-in field that cannot be removed
example: false
Receipt:
type: object
description: |
A flat JSON object. Native fields (productName, price, etc.) are stored as
dedicated columns. Any additional keys are user-defined fields stored as JSON
internally but returned at the same level. Every non-native key must be
registered in the meta table.
properties:
id:
type: string
format: uuid
example: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
productName:
type: string
example: "Milk 2%"
purchaseDate:
type: string
description: Purchase date in Y.M.D format
example: "2025.04.05"
price:
type: string
example: "5.49CAD"
amount:
type: string
description: Amount in the format of number or number(unit)
example: "2lb"
storeName:
type: string
example: "Costco"
latitude:
type: number
format: float
nullable: true
example: 49.2827
longitude:
type: number
format: float
nullable: true
example: -123.1207
uploadTime:
type: integer
description: Upload time as Unix epoch seconds (set by server)
example: 1770620311
userId:
type: string
format: uuid
description: ID of the user who created this record (set by server)
example: "550e8400-e29b-41d4-a716-446655440000"
additionalProperties:
description: User-defined fields registered in the meta table
example:
id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
productName: "Milk 2%"
purchaseDate: "2025.04.05"
price: "5.49CAD"
amount: "2lb"
storeName: "Costco"
latitude: 49.2827
longitude: -123.1207
brand: "Kirkland"
uploadTime: 1770620311
userId: "550e8400-e29b-41d4-a716-446655440000"
paths:
# ── Users ──────────────────────────────────────────────────────────────
/users:
post:
summary: Register a new user
description: |
Creates a new user account. The account is active immediately (open registration).
tags: [Users]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [username, password]
properties:
username:
type: string
example: "alice"
password:
type: string
minLength: 8
example: "password123"
responses:
"201":
description: User created
content:
application/json:
schema:
$ref: "#/components/schemas/User"
"400":
description: Invalid request (missing fields or password too short)
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409":
description: Username already exists
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
get:
summary: List all users
description: Returns a paginated list of all registered user accounts. Admin only.
tags: [Admin - Users]
security:
- bearerAuth: []
parameters:
- $ref: "#/components/parameters/offsetParam"
- $ref: "#/components/parameters/limitParam"
- name: sort_by
in: query
required: false
schema:
type: string
enum: [created_at, username, role]
default: created_at
description: Field to sort by
- name: sort_order
in: query
required: false
schema:
type: string
enum: [asc, desc]
default: desc
description: Sort direction
responses:
"200":
description: Paginated list of users
content:
application/json:
schema:
$ref: "#/components/schemas/UserPage"
"400":
description: Invalid pagination parameters
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"403":
description: Admin access required
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
# ── Sessions ───────────────────────────────────────────────────────────
/auth/login:
post:
summary: Login
description: |
Verifies the username and password and returns a JWT access token and a refresh token.
The access token is valid for the duration set in `auth.access_token_exp` (default 1 hour).
The refresh token is valid for `auth.refresh_token_exp` (default 7 days).
tags: [Sessions]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [username, password]
properties:
username:
type: string
example: "alice"
password:
type: string
example: "password123"
responses:
"200":
description: Login successful
content:
application/json:
schema:
$ref: "#/components/schemas/TokenResponse"
"400":
description: Missing fields
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Invalid username or password
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/auth/logout:
post:
summary: Logout
description: |
Revokes the refresh token provided in the request body.
The access token will expire naturally. If no refresh token is provided, the endpoint
still returns 200 — the access token will simply expire on its own.
tags: [Sessions]
security:
- bearerAuth: []
requestBody:
required: false
content:
application/json:
schema:
type: object
properties:
refresh_token:
type: string
example: "eyJhbGciOiJIUzI1NiIs..."
responses:
"200":
description: Logged out
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "logged out"
"401":
description: Missing or invalid access token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/auth/refresh:
post:
summary: Refresh access token
description: |
Exchanges a valid refresh token for a new access token and refresh token pair.
The old refresh token is immediately invalidated (rotation).
tags: [Sessions]
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [refresh_token]
properties:
refresh_token:
type: string
example: "eyJhbGciOiJIUzI1NiIs..."
responses:
"200":
description: New token pair issued
content:
application/json:
schema:
$ref: "#/components/schemas/TokenResponse"
"401":
description: Invalid or expired refresh token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/auth/me:
get:
summary: Get current user
description: Returns the ID and role of the currently authenticated user, read from the access token claims.
tags: [Sessions]
security:
- bearerAuth: []
responses:
"200":
description: Current user info
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: "550e8400-e29b-41d4-a716-446655440000"
role:
type: string
enum: [admin, user]
example: "user"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
# ── Meta (authenticated) ───────────────────────────────────────────────
/meta:
get:
summary: List all fields
description: |
Returns a paginated list of all registered field definitions, both native (built-in)
and user-defined. Any authenticated user can call this endpoint.
Default sort is `name asc` (alphabetical).
tags: [Meta]
security:
- bearerAuth: []
parameters:
- $ref: "#/components/parameters/offsetParam"
- $ref: "#/components/parameters/limitParam"
- name: sort_by
in: query
required: false
schema:
type: string
enum: [name]
default: name
description: Field to sort by
- name: sort_order
in: query
required: false
schema:
type: string
enum: [asc, desc]
default: asc
description: Sort direction (default asc for this endpoint)
responses:
"200":
description: Paginated list of fields
content:
application/json:
schema:
$ref: "#/components/schemas/MetaFieldPage"
"400":
description: Invalid pagination parameters
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Register a new field
description: |
Registers a new user-defined field. Once registered, receipts can include
this field in their `extras` object. Any authenticated user can register fields.
tags: [Meta]
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [fieldName, description, type]
properties:
fieldName:
type: string
example: "brand"
description:
type: string
example: "brand of the product"
type:
type: string
example: "string"
responses:
"201":
description: Field registered
content:
application/json:
schema:
$ref: "#/components/schemas/MetaField"
"400":
description: Missing required fields
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409":
description: Field already exists
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
# ── Receipts (authenticated) ───────────────────────────────────────────
/receipts:
post:
summary: Create a receipt
description: |
Creates a new purchase record for the authenticated user.
The server sets `id`, `uploadTime`, and `userId` automatically.
Any keys in `extras` must be registered in the meta table or the request is rejected.
tags: [Receipts]
security:
- bearerAuth: []
requestBody:
required: true
description: |
A flat JSON object. Include native fields and any registered
user-defined fields at the same level — no `extras` wrapper needed.
content:
application/json:
schema:
type: object
required: [productName, purchaseDate, price, amount, storeName]
properties:
productName:
type: string
example: "Milk 2%"
purchaseDate:
type: string
example: "2025.04.05"
price:
type: string
example: "5.49CAD"
amount:
type: string
example: "1"
storeName:
type: string
example: "Costco"
latitude:
type: number
format: float
example: 49.2827
longitude:
type: number
format: float
example: -123.1207
additionalProperties:
description: User-defined fields registered in the meta table
responses:
"201":
description: Receipt created
content:
application/json:
schema:
$ref: "#/components/schemas/Receipt"
"400":
description: Missing required fields or unregistered extra field
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
get:
summary: List own receipts
description: |
Returns a paginated list of receipts belonging to the authenticated user.
Default sort is `created_at desc` (newest first).
The `created_at` sort param maps to the `upload_time` column internally.
tags: [Receipts]
security:
- bearerAuth: []
parameters:
- $ref: "#/components/parameters/offsetParam"
- $ref: "#/components/parameters/limitParam"
- name: sort_by
in: query
required: false
schema:
type: string
enum: [created_at, purchase_date, price, store_name, product_name]
default: created_at
description: Field to sort by
- name: sort_order
in: query
required: false
schema:
type: string
enum: [asc, desc]
default: desc
description: Sort direction
responses:
"200":
description: Paginated list of receipts
content:
application/json:
schema:
$ref: "#/components/schemas/ReceiptPage"
"400":
description: Invalid pagination parameters
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/receipts/{id}:
get:
summary: Get a receipt by ID
description: Returns a single receipt by its ID.
tags: [Receipts]
security:
- bearerAuth: []
parameters:
- name: id
in: path
required: true
schema:
type: string
example: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
responses:
"200":
description: Receipt found
content:
application/json:
schema:
$ref: "#/components/schemas/Receipt"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"404":
description: Receipt not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
delete:
summary: Delete a receipt
description: Deletes a receipt by its ID.
tags: [Receipts]
security:
- bearerAuth: []
parameters:
- name: id
in: path
required: true
schema:
type: string
example: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
responses:
"200":
description: Receipt deleted
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "receipt deleted"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"404":
description: Receipt not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
# ── Users (admin only) ─────────────────────────────────────────────────
/users/{id}:
delete:
summary: Delete a user
description: |
Deletes a user account and immediately revokes all their active refresh tokens.
Admin only.
tags: [Admin - Users]
security:
- bearerAuth: []
parameters:
- name: id
in: path
required: true
schema:
type: string
description: The user ID to delete
example: "550e8400-e29b-41d4-a716-446655440000"
responses:
"200":
description: User deleted
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "user deleted"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"403":
description: Admin access required
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"404":
description: User not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
# ── Admin — Meta ───────────────────────────────────────────────────────
/meta/{fieldName}:
put:
summary: Update a field description
description: |
Updates the description of an existing field (native or user-defined). Admin only.
tags: [Admin - Meta]
security:
- bearerAuth: []
parameters:
- name: fieldName
in: path
required: true
schema:
type: string
example: "brand"
requestBody:
required: true
content:
application/json:
schema:
type: object
required: [description]
properties:
description:
type: string
example: "brand or manufacturer of the product"
responses:
"200":
description: Description updated
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "description updated"
"400":
description: Missing description
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"401":
description: Missing or invalid token
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"403":
description: Admin access required
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"404":
description: Field not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"