Trying to upsert a front account using the sdk. Its (like most of the API I have to say extremely cumbersome).
This is my code snippet:
async function upsertFrontAccount(validatedAddress: any): Promise<any> {
try {
return await sdk.createAccount({
name: validatedAddress.company || "Unknown Company",
domains: :validatedAddress.emailDomain],
custom_fields: {
Adresse: validatedAddress.address,
PLZ: validatedAddress.zip,
Stadt: validatedAddress.city,
Land: validatedAddress.country || "Germany",
Umsatzsteuernummer: validatedAddress.vatId,
Website: validatedAddress.url,
},
});
} catch (error) {
console.log("ERROR ON UPDATE", error.res);
if (error.status === 409) {
try {
await sdk.updateAccount(
{
account_id: `alt:domain:${validatedAddress.emailDomain}`,
},
{
name: validatedAddress.company || "Unknown Company",
domains: :validatedAddress.emailDomain],
custom_fields: {
Adresse: validatedAddress.address,
PLZ: validatedAddress.zip,
Stadt: validatedAddress.city,
Land: validatedAddress.country || "Germany",
Umsatzsteuernummer: validatedAddress.vatId,
Website: validatedAddress.url,
},
}
);
} catch (error) {
console.log("ERROR ON UPDATE", error);
}
console.log("UPDATE", error.res.data);
return;
}
console.log("ERROR", error.status);
throw new Error(`Front SDK call failed: ${error.message}`);
}
}
In a nutshell:
The create() call fails if it exists with a 409 => conflict. In that case I want to update() the account using the updateAccount method and pass the email domain like so:
account_id: `alt:domain:${validatedAddress.emailDomain}`,
Now it always complains with a 404 - Not found
Does not make sense to me that first there is a conflict and then right after not found. I mean its either one or the other.
I suppose I pass the alt:domain incorrectly but to do not know how to proceed. Please help me here.
Thanks
PS: Why does Front not have a single Upsert() method? Its incredibly annoying to create, then update in case of a duplicate error….