File size: 1,350 Bytes
0e759d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { logger } from "../../lib/logger";
import { getBillingQueue } from "../queue-service";
import { v4 as uuidv4 } from "uuid";
import * as Sentry from "@sentry/node";

/**
 * Adds a job to the billing queue to trigger batch processing
 * This can be used when we want to ensure billing is processed without waiting for the next interval
 */
export async function addBillingBatchJob() {
  try {
    const jobId = uuidv4();
    logger.info("Adding billing batch job to queue", { jobId });
    
    await getBillingQueue().add(
      "process-batch",
      {
        timestamp: new Date().toISOString(),
      },
      {
        jobId,
        priority: 10,
      }
    );
    
    return { success: true, jobId };
  } catch (error) {
    logger.error("Error adding billing batch job", { error });
    Sentry.captureException(error, {
      data: {
        operation: "add_billing_batch_job"
      }
    });
    return { success: false, error };
  }
}

/**
 * Trigger immediate processing of any pending billing operations
 * This is useful for ensuring billing operations are processed without delay
 */
export async function triggerImmediateBillingProcess() {
  try {
    return await addBillingBatchJob();
  } catch (error) {
    logger.error("Error triggering immediate billing process", { error });
    return { success: false, error };
  }
}