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