fix: update RPC calls to use company_id instead of user_id for invoice and arrival number generation (#154)
This commit is contained in:
@@ -56,7 +56,7 @@ export async function POST(
|
||||
|
||||
// Generate real invoice number
|
||||
const { data: invoiceNumber } = await supabase.rpc('generate_invoice_number', {
|
||||
p_user_id: user.id,
|
||||
p_company_id: companyId,
|
||||
})
|
||||
|
||||
// Create the real invoice
|
||||
|
||||
@@ -166,12 +166,12 @@ export async function POST(request: Request) {
|
||||
let invoiceNumber: string
|
||||
if (documentType === 'delivery_note') {
|
||||
const { data: dnNumber } = await supabase.rpc('generate_delivery_note_number', {
|
||||
p_user_id: user.id,
|
||||
p_company_id: companyId,
|
||||
})
|
||||
invoiceNumber = dnNumber
|
||||
} else {
|
||||
const { data: baseNumber } = await supabase.rpc('generate_invoice_number', {
|
||||
p_user_id: user.id,
|
||||
p_company_id: companyId,
|
||||
})
|
||||
invoiceNumber = documentType === 'proforma'
|
||||
? `PF-${baseNumber}`
|
||||
|
||||
@@ -340,7 +340,7 @@ async function commitCreateInvoice(
|
||||
|
||||
// Generate invoice number
|
||||
const { data: invoiceNumber } = await supabase.rpc('generate_invoice_number', {
|
||||
p_user_id: userId,
|
||||
p_company_id: companyId,
|
||||
})
|
||||
|
||||
// Create invoice
|
||||
|
||||
@@ -73,7 +73,7 @@ export async function POST() {
|
||||
|
||||
// 3. Seed chart of accounts via RPC
|
||||
const { error: coaError } = await supabase.rpc('seed_chart_of_accounts', {
|
||||
p_user_id: userId,
|
||||
p_company_id: companyId,
|
||||
p_entity_type: 'enskild_firma',
|
||||
})
|
||||
if (coaError) throw coaError
|
||||
@@ -294,7 +294,7 @@ export async function POST() {
|
||||
|
||||
// 9. Create journal entries (inserted directly, not via engine, to avoid event emission)
|
||||
const { data: voucherNum1 } = await supabase.rpc('next_voucher_number', {
|
||||
p_user_id: userId,
|
||||
p_company_id: companyId,
|
||||
p_fiscal_period_id: fiscalPeriod.id,
|
||||
p_series: 'A',
|
||||
})
|
||||
@@ -320,7 +320,7 @@ export async function POST() {
|
||||
if (je1Error) throw je1Error
|
||||
|
||||
const { data: voucherNum2 } = await supabase.rpc('next_voucher_number', {
|
||||
p_user_id: userId,
|
||||
p_company_id: companyId,
|
||||
p_fiscal_period_id: fiscalPeriod.id,
|
||||
p_series: 'A',
|
||||
})
|
||||
|
||||
@@ -44,7 +44,7 @@ export async function POST(
|
||||
|
||||
// Get next arrival number
|
||||
const { data: arrivalNum } = await supabase
|
||||
.rpc('get_next_arrival_number', { p_user_id: user.id })
|
||||
.rpc('get_next_arrival_number', { p_company_id: companyId })
|
||||
|
||||
// Create credit note invoice (negative amounts)
|
||||
const { data: creditNote, error: creditError } = await supabase
|
||||
|
||||
@@ -123,6 +123,8 @@ export async function POST(
|
||||
const { error: paymentError } = await supabase
|
||||
.from('supplier_invoice_payments')
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
company_id: companyId,
|
||||
supplier_invoice_id: id,
|
||||
payment_date: paymentDate,
|
||||
amount: paymentAmount,
|
||||
|
||||
@@ -75,7 +75,7 @@ export async function POST(request: Request) {
|
||||
|
||||
// Get next arrival number
|
||||
const { data: arrivalNum, error: arrivalError } = await supabase
|
||||
.rpc('get_next_arrival_number', { p_user_id: user.id })
|
||||
.rpc('get_next_arrival_number', { p_company_id: companyId })
|
||||
|
||||
if (arrivalError) {
|
||||
return NextResponse.json({ error: 'Failed to get arrival number' }, { status: 500 })
|
||||
|
||||
@@ -76,6 +76,7 @@ export interface CategorizationSuggestion {
|
||||
export interface TrackingContext {
|
||||
supabase: SupabaseClient
|
||||
userId: string
|
||||
companyId?: string
|
||||
}
|
||||
|
||||
export interface CategorizationProvider {
|
||||
@@ -336,7 +337,7 @@ ${transactionList}`
|
||||
inputTokens: message.usage.input_tokens,
|
||||
outputTokens: message.usage.output_tokens,
|
||||
model: this.model,
|
||||
})
|
||||
}, tracking.companyId)
|
||||
}
|
||||
|
||||
// Extract tool_use block from response
|
||||
|
||||
@@ -36,7 +36,7 @@ export interface ChatResult {
|
||||
export async function generateChatResponse(
|
||||
userMessage: string,
|
||||
conversationHistory: ChatMessage[],
|
||||
tracking?: { supabase: SupabaseClient; userId: string }
|
||||
tracking?: { supabase: SupabaseClient; userId: string; companyId?: string }
|
||||
): Promise<ChatResult> {
|
||||
// 1. Retrieve relevant documents
|
||||
const relevantDocs = await retrieveRelevantDocuments(userMessage)
|
||||
@@ -81,7 +81,7 @@ export async function generateChatResponse(
|
||||
inputTokens: response.usage_metadata.input_tokens ?? 0,
|
||||
outputTokens: response.usage_metadata.output_tokens ?? 0,
|
||||
model: CHATBOT_CONFIG.model,
|
||||
})
|
||||
}, tracking.companyId)
|
||||
}
|
||||
|
||||
// 7. Extract content and sources
|
||||
|
||||
@@ -403,7 +403,7 @@ export async function executeMigration(options: MigrationOptions): Promise<Migra
|
||||
|
||||
// Get next arrival number (ankomstnummer) — required NOT NULL column
|
||||
const { data: arrivalNum, error: arrivalError } = await supabase
|
||||
.rpc('get_next_arrival_number', { p_user_id: userId })
|
||||
.rpc('get_next_arrival_number', { p_company_id: userId })
|
||||
|
||||
if (arrivalError || arrivalNum == null) {
|
||||
console.error(`[migration] Supplier invoice ${inv.invoiceNumber} skipped — could not get arrival number:`, arrivalError?.message)
|
||||
|
||||
@@ -166,6 +166,7 @@ async function uploadAndCreateInboxItem(
|
||||
.from('invoice_inbox_items')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
company_id: companyId,
|
||||
status: 'processing',
|
||||
source: 'upload',
|
||||
document_id: document.id,
|
||||
@@ -818,7 +819,7 @@ async function handleConfirmInboxItem(
|
||||
|
||||
// Get next arrival number
|
||||
const { data: arrivalNum, error: arrivalError } = await supabase
|
||||
.rpc('get_next_arrival_number', { p_user_id: userId })
|
||||
.rpc('get_next_arrival_number', { p_company_id: companyId })
|
||||
|
||||
if (arrivalError) {
|
||||
console.error('[invoice-inbox] Confirm: arrival number error:', arrivalError)
|
||||
|
||||
@@ -16,12 +16,14 @@ export function trackTokenUsage(
|
||||
supabase: SupabaseClient,
|
||||
userId: string,
|
||||
extensionId: string,
|
||||
usage: TokenUsage
|
||||
usage: TokenUsage,
|
||||
companyId?: string
|
||||
): void {
|
||||
supabase
|
||||
.from('ai_usage_tracking')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
company_id: companyId,
|
||||
extension_id: extensionId,
|
||||
model: usage.model,
|
||||
input_tokens: usage.inputTokens,
|
||||
|
||||
@@ -91,6 +91,7 @@ export async function uploadDocument(
|
||||
.from('document_attachments')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
company_id: companyId,
|
||||
storage_path: storagePath,
|
||||
file_name: file.name,
|
||||
file_size_bytes: file.buffer.byteLength,
|
||||
|
||||
@@ -210,6 +210,7 @@ export async function processOverdueReminders(): Promise<ProcessRemindersResult>
|
||||
.insert({
|
||||
invoice_id: invoice.id,
|
||||
user_id: invoice.user_id,
|
||||
company_id: invoice.company_id,
|
||||
reminder_level: reminderLevel,
|
||||
email_to: customer.email
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user