ClaraSu commited on
Commit
489d61b
·
verified ·
1 Parent(s): ddde387

Upload folder using huggingface_hub

Browse files
apps/api/src/proxy.ts CHANGED
@@ -1,7 +1,9 @@
1
  import 'dotenv/config'
2
  import express from 'express'
 
3
  import path from 'path'
4
  import { authMiddleware } from './middleware/authMiddleware'
 
5
  import { authRouter } from './services/auth/auth.routes'
6
  import { chatRouter } from './services/chat/chat.routes'
7
  import { expenseRouter } from './services/expense/router'
@@ -43,6 +45,17 @@ app.use('/v1', chatRouter)
43
  app.use('/v1/expense', expenseRouter)
44
  app.use('/v1/slip-queue', slipQueueRouter)
45
 
 
 
 
 
 
 
 
 
 
 
 
46
  const PORT = parseInt(process.env['PORT'] ?? '7860', 10)
47
  const FOUR_MINUTES = 4 * 60 * 1000
48
 
 
1
  import 'dotenv/config'
2
  import express from 'express'
3
+ import type { Request, Response, NextFunction } from 'express'
4
  import path from 'path'
5
  import { authMiddleware } from './middleware/authMiddleware'
6
+ import { logger } from './logger.js'
7
  import { authRouter } from './services/auth/auth.routes'
8
  import { chatRouter } from './services/chat/chat.routes'
9
  import { expenseRouter } from './services/expense/router'
 
45
  app.use('/v1/expense', expenseRouter)
46
  app.use('/v1/slip-queue', slipQueueRouter)
47
 
48
+ app.use((err: unknown, req: Request, res: Response, _next: NextFunction) => {
49
+ logger.error('unhandled_route_error', {
50
+ method: req.method,
51
+ path: req.path,
52
+ error: err instanceof Error ? err.message : String(err),
53
+ stack: err instanceof Error ? err.stack : undefined,
54
+ })
55
+ if (res.headersSent) return
56
+ res.status(500).json({ error: 'Internal server error' })
57
+ })
58
+
59
  const PORT = parseInt(process.env['PORT'] ?? '7860', 10)
60
  const FOUR_MINUTES = 4 * 60 * 1000
61
 
apps/api/src/services/expense/slip-queue/slip-queue.service.ts CHANGED
@@ -1,4 +1,5 @@
1
  import type { ISlipQueueRepository, IReceiptParser, IImageStore, SlipQueueItem } from '../ports/index.js'
 
2
 
3
  export class SlipQueueService {
4
  constructor(
@@ -26,8 +27,8 @@ export class SlipQueueService {
26
  let failed = 0
27
 
28
  for (const slip of pending) {
29
- await this.slipRepo.update(slip.id, { status: 'processing' })
30
  try {
 
31
  const imageBase64 = await this.imageStore.get(slip.image_base64)
32
  const parsed = await this.parser.parse({ imageBase64 })
33
  if (!parsed || !parsed.amount) {
@@ -41,21 +42,29 @@ export class SlipQueueService {
41
  succeeded++
42
  } catch (err: unknown) {
43
  const msg = err instanceof Error ? err.message : String(err)
 
44
  const newRetryCount = slip.retry_count + 1
45
- if (newRetryCount < 3) {
46
- await this.slipRepo.update(slip.id, {
47
- status: 'pending',
48
- retry_count: newRetryCount,
49
- error_message: msg,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  })
51
- } else {
52
- await this.slipRepo.update(slip.id, {
53
- status: 'failed',
54
- retry_count: newRetryCount,
55
- error_message: msg,
56
- processed_at: new Date().toISOString(),
57
- })
58
- failed++
59
  }
60
  }
61
  }
 
1
  import type { ISlipQueueRepository, IReceiptParser, IImageStore, SlipQueueItem } from '../ports/index.js'
2
+ import { logger } from '../../../logger.js'
3
 
4
  export class SlipQueueService {
5
  constructor(
 
27
  let failed = 0
28
 
29
  for (const slip of pending) {
 
30
  try {
31
+ await this.slipRepo.update(slip.id, { status: 'processing' })
32
  const imageBase64 = await this.imageStore.get(slip.image_base64)
33
  const parsed = await this.parser.parse({ imageBase64 })
34
  if (!parsed || !parsed.amount) {
 
42
  succeeded++
43
  } catch (err: unknown) {
44
  const msg = err instanceof Error ? err.message : String(err)
45
+ logger.warn('slip_queue_item_failed', { slipId: slip.id, error: msg })
46
  const newRetryCount = slip.retry_count + 1
47
+ try {
48
+ if (newRetryCount < 3) {
49
+ await this.slipRepo.update(slip.id, {
50
+ status: 'pending',
51
+ retry_count: newRetryCount,
52
+ error_message: msg,
53
+ })
54
+ } else {
55
+ await this.slipRepo.update(slip.id, {
56
+ status: 'failed',
57
+ retry_count: newRetryCount,
58
+ error_message: msg,
59
+ processed_at: new Date().toISOString(),
60
+ })
61
+ failed++
62
+ }
63
+ } catch (updateErr: unknown) {
64
+ logger.error('slip_queue_item_update_failed', {
65
+ slipId: slip.id,
66
+ error: updateErr instanceof Error ? updateErr.message : String(updateErr),
67
  })
 
 
 
 
 
 
 
 
68
  }
69
  }
70
  }