feat: store firstName and lastName in the Odoo mailing contact database
This commit is contained in:
parent
efaec2fe11
commit
50e222fd2a
|
@ -4,7 +4,8 @@ import * as yup from 'yup'
|
||||||
import { settle } from '../../../utils/promise.utils'
|
import { settle } from '../../../utils/promise.utils'
|
||||||
|
|
||||||
const formSchema = yup.object().shape({
|
const formSchema = yup.object().shape({
|
||||||
name: yup.string().optional(),
|
firstName: yup.string().optional(),
|
||||||
|
lastName: yup.string().optional(),
|
||||||
email: yup.string().email().required(),
|
email: yup.string().email().required(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -40,13 +41,29 @@ const isSubscribed = async (
|
||||||
return [!!subscription, subscription]
|
return [!!subscription, subscription]
|
||||||
}
|
}
|
||||||
|
|
||||||
const createContact = async (client: Odoo, name: string, email: string) => {
|
const createContact = async (
|
||||||
let [contact] = await client.search('mailing.contact', ['email', '=', email])
|
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) {
|
if (!contact) {
|
||||||
contact = await client.create('mailing.contact', {
|
contact = await client.create('mailing.contact', {
|
||||||
name: name,
|
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(
|
const contact = await createContact(client, payload)
|
||||||
client,
|
|
||||||
payload.name || payload.email,
|
|
||||||
payload.email,
|
|
||||||
)
|
|
||||||
await subscribe(client, contact, ODOO_MAILING_LIST_ID)
|
await subscribe(client, contact, ODOO_MAILING_LIST_ID)
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
|
|
|
@ -80,13 +80,17 @@ export class ApiService {
|
||||||
return { data: { posts: [], blocks: [] }, errors: JSON.stringify(e) }
|
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', {
|
const res = await fetch('/api/newsletter/subscribe', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ email, name }),
|
body: JSON.stringify(payload),
|
||||||
})
|
})
|
||||||
|
|
||||||
return res.json()
|
return res.json()
|
||||||
|
|
Loading…
Reference in New Issue