-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathinngest.js
More file actions
63 lines (53 loc) · 1.57 KB
/
inngest.js
File metadata and controls
63 lines (53 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Inngest } from "inngest";
import connectDB from "./db";
import User from "@/models/User";
// Create a client to send and receive events
export const inngest = new Inngest({ id: "quickcart-next" });
// Inngest Function to save use data to a database
export const syncUserCreation = inngest.createFunction(
{
id:'sync-user-from-clerk'
},
{ event:'clerk/user.created' },
async ({event}) =>{
const { id, first_name, last_name, email_addresses, image_url } = event.data
const userData ={
_id:id,
email:email_addresses[0].email_address,
name:first_name + ' ' + last_name,
imageUrl:image_url
}
await connectDB()
await User.create(userData)
}
)
// Inngest Function to update user data in a database
export const syncUserUpdation = inngest.createFunction(
{
id:'update-user-from-clerk'
},
{ event:'clerk/user.updated' },
async ({event}) =>{
const { id, first_name, last_name, email_addresses, image_url } = event.data
const userData ={
_id:id,
email:email_addresses[0].email_address,
name:first_name + ' ' + last_name,
imageUrl:image_url
}
await connectDB()
await User.findByIdAndUpdate(id,userData )
}
)
// Inngest Function to delete user from the databas
export const syncUserDeletion = inngest.createFunction(
{
id:'delete-user-with-clerk'
},
{event:'clerk/user.deleted'},
async ({event}) =>{
const {id} = event.data
await connectDB()
await User.findByIdAndDelete(id)
}
)