dolibarr user
This commit is contained in:
parent
f0a08adb2c
commit
0dcb2ad5c5
@ -495,8 +495,8 @@ export async function POST(req: Request) {
|
|||||||
console.log(`User already exists in Dolibarr with ID: ${existingUser.id}`);
|
console.log(`User already exists in Dolibarr with ID: ${existingUser.id}`);
|
||||||
dolibarrUserId = existingUser.id;
|
dolibarrUserId = existingUser.id;
|
||||||
} else {
|
} else {
|
||||||
// Create user in Dolibarr
|
// Create user account in Dolibarr
|
||||||
console.log('Creating new user in Dolibarr with data:', {
|
console.log('Creating new user account in Dolibarr with data:', {
|
||||||
username: data.username,
|
username: data.username,
|
||||||
email: data.email,
|
email: data.email,
|
||||||
name: `${data.firstName} ${data.lastName}`
|
name: `${data.firstName} ${data.lastName}`
|
||||||
@ -511,10 +511,10 @@ export async function POST(req: Request) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (dolibarrResult.success) {
|
if (dolibarrResult.success) {
|
||||||
console.log(`User created in Dolibarr with ID: ${dolibarrResult.id}`);
|
console.log(`User account created in Dolibarr with ID: ${dolibarrResult.id}`);
|
||||||
dolibarrUserId = dolibarrResult.id;
|
dolibarrUserId = dolibarrResult.id;
|
||||||
} else {
|
} else {
|
||||||
console.error("Dolibarr user creation failed:", dolibarrResult.error);
|
console.error("Dolibarr user account creation failed:", dolibarrResult.error);
|
||||||
// We don't return an error here since Keycloak user was created successfully
|
// We don't return an error here since Keycloak user was created successfully
|
||||||
// We just log the error and continue
|
// We just log the error and continue
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { DolibarrErrorResponse, DolibarrThirdParty } from "@/app/types/dolibarr";
|
import { DolibarrErrorResponse, DolibarrThirdParty, DolibarrUser } from "@/app/types/dolibarr";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a user in Dolibarr
|
* Create a user in Dolibarr
|
||||||
@ -32,23 +32,28 @@ export async function createDolibarrUser(userData: {
|
|||||||
: `${process.env.DOLIBARR_API_URL}/`;
|
: `${process.env.DOLIBARR_API_URL}/`;
|
||||||
|
|
||||||
console.log(`Creating Dolibarr user for ${userData.email}`);
|
console.log(`Creating Dolibarr user for ${userData.email}`);
|
||||||
console.log('Full API URL:', apiUrl + 'thirdparties');
|
console.log('Full API URL:', apiUrl + 'users');
|
||||||
console.log('API Key (first 5 chars):', process.env.DOLIBARR_API_KEY.substring(0, 5) + '...');
|
console.log('API Key (first 5 chars):', process.env.DOLIBARR_API_KEY.substring(0, 5) + '...');
|
||||||
|
|
||||||
|
// Format the user data for Dolibarr user creation
|
||||||
const requestBody = {
|
const requestBody = {
|
||||||
name: `${userData.firstName} ${userData.lastName}`,
|
login: userData.username,
|
||||||
name_alias: userData.username,
|
lastname: userData.lastName,
|
||||||
|
firstname: userData.firstName,
|
||||||
|
password: userData.password,
|
||||||
email: userData.email,
|
email: userData.email,
|
||||||
client: '1', // Mark as customer
|
admin: 0, // Not an administrator
|
||||||
code_client: 'auto', // Auto-generate client code
|
employee: 1, // Is an employee
|
||||||
|
signature: `${userData.firstName} ${userData.lastName}`,
|
||||||
note_private: 'Created via API integration from platform',
|
note_private: 'Created via API integration from platform',
|
||||||
status: '1', // Active
|
entity: 1, // Main entity
|
||||||
|
status: 1 // Active
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('Request body:', JSON.stringify(requestBody, null, 2));
|
console.log('Request body:', JSON.stringify(requestBody, null, 2));
|
||||||
|
|
||||||
// Create the user in Dolibarr as a thirdparty/customer
|
// Create the user in Dolibarr as a user account
|
||||||
const response = await fetch(`${apiUrl}thirdparties`, {
|
const response = await fetch(`${apiUrl}users`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'DOLAPIKEY': process.env.DOLIBARR_API_KEY,
|
'DOLAPIKEY': process.env.DOLIBARR_API_KEY,
|
||||||
@ -121,10 +126,11 @@ export async function checkDolibarrUserExists(email: string): Promise<{ exists:
|
|||||||
? process.env.DOLIBARR_API_URL
|
? process.env.DOLIBARR_API_URL
|
||||||
: `${process.env.DOLIBARR_API_URL}/`;
|
: `${process.env.DOLIBARR_API_URL}/`;
|
||||||
|
|
||||||
const queryUrl = `${apiUrl}thirdparties?sortfield=t.rowid&sortorder=ASC&limit=1&sqlfilters=(t.email:=:'${encodeURIComponent(email)}')`;
|
// Use the users endpoint instead of thirdparties
|
||||||
|
const queryUrl = `${apiUrl}users?sortfield=t.rowid&sortorder=ASC&limit=1&sqlfilters=(t.email:=:'${encodeURIComponent(email)}')`;
|
||||||
console.log('Full search URL:', queryUrl);
|
console.log('Full search URL:', queryUrl);
|
||||||
|
|
||||||
// Search for thirdparty/customer with matching email
|
// Search for user with matching email
|
||||||
const response = await fetch(queryUrl, {
|
const response = await fetch(queryUrl, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
@ -145,7 +151,7 @@ export async function checkDolibarrUserExists(email: string): Promise<{ exists:
|
|||||||
return { exists: false };
|
return { exists: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json() as DolibarrThirdParty[];
|
const data = await response.json() as DolibarrUser[];
|
||||||
console.log('Dolibarr user search response:', data);
|
console.log('Dolibarr user search response:', data);
|
||||||
|
|
||||||
// If we got results, user exists
|
// If we got results, user exists
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user