feat: store firstName and lastName in the Odoo mailing contact database

This commit is contained in:
Hossein Mehrabi 2023-10-26 16:47:18 +03:30
parent efaec2fe11
commit 50e222fd2a
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
2 changed files with 28 additions and 11 deletions

View File

@ -4,7 +4,8 @@ import * as yup from 'yup'
import { settle } from '../../../utils/promise.utils'
const formSchema = yup.object().shape({
name: yup.string().optional(),
firstName: yup.string().optional(),
lastName: yup.string().optional(),
email: yup.string().email().required(),
})
@ -40,13 +41,29 @@ const isSubscribed = async (
return [!!subscription, subscription]
}
const createContact = async (client: Odoo, name: string, email: string) => {
let [contact] = await client.search('mailing.contact', ['email', '=', email])
const createContact = async (
client: Odoo,
payload: {
firstName?: string
lastName?: string
email: string
},
) => {
const name =
[payload.firstName, payload.lastName].join(' ').trim() || payload.email
let [contact] = await client.search('mailing.contact', [
'email',
'=',
payload.email,
])
if (!contact) {
contact = await client.create('mailing.contact', {
name: name,
email: email,
email: payload.email,
x_first_name: payload.firstName,
x_last_name: payload.lastName,
})
}
@ -96,11 +113,7 @@ export default async function handler(
})
}
const contact = await createContact(
client,
payload.name || payload.email,
payload.email,
)
const contact = await createContact(client, payload)
await subscribe(client, contact, ODOO_MAILING_LIST_ID)
res.status(200).json({

View File

@ -80,13 +80,17 @@ export class ApiService {
return { data: { posts: [], blocks: [] }, errors: JSON.stringify(e) }
})
subscribeToMailingList = async (email: string, name?: string) => {
subscribeToMailingList = async (payload: {
email: string
firstName?: string
lastName?: string
}) => {
const res = await fetch('/api/newsletter/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, name }),
body: JSON.stringify(payload),
})
return res.json()