create profile if not exists
This commit is contained in:
@@ -14,9 +14,11 @@
|
||||
"dependencies": {
|
||||
"@mixan/types": "workspace:*",
|
||||
"@prisma/client": "^5.4.2",
|
||||
"@types/ramda": "^0.29.6",
|
||||
"express": "^4.18.2",
|
||||
"morgan": "^1.10.0",
|
||||
"prisma": "^5.4.2",
|
||||
"ramda": "^0.29.1",
|
||||
"random-animal-name": "^0.1.1",
|
||||
"uuid": "^9.0.1"
|
||||
},
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import {NextFunction, Response, Router} from 'express'
|
||||
import { db } from '../db';
|
||||
import { MixanRequest } from '../types/express';
|
||||
import { EventPayload } from '@mixan/types';
|
||||
import { getEvents } from '../services/event';
|
||||
import { success } from '../responses/success';
|
||||
|
||||
const router = Router();
|
||||
import { NextFunction, Response, Router } from 'express'
|
||||
import { db } from '../db'
|
||||
import { MixanRequest } from '../types/express'
|
||||
import { EventPayload } from '@mixan/types'
|
||||
import { getEvents } from '../services/event'
|
||||
import { success } from '../responses/success'
|
||||
import { getProfile } from '../services/profile'
|
||||
import { uniq } from 'ramda'
|
||||
const router = Router()
|
||||
|
||||
type PostRequest = MixanRequest<Array<EventPayload>>
|
||||
|
||||
@@ -18,24 +19,48 @@ router.get('/events', async (req, res, next) => {
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/events', async (req: PostRequest, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const projectId = req.client.project_id
|
||||
|
||||
await db.event.createMany({
|
||||
data: req.body.map((event) => ({
|
||||
name: event.name,
|
||||
properties: event.properties,
|
||||
createdAt: event.time,
|
||||
project_id: projectId,
|
||||
profile_id: event.profileId,
|
||||
}))
|
||||
})
|
||||
|
||||
res.status(201).json(success())
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
})
|
||||
router.post(
|
||||
'/events',
|
||||
async (req: PostRequest, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const projectId = req.client.project_id
|
||||
|
||||
export default router
|
||||
const profileIds = uniq(
|
||||
req.body
|
||||
.map((event) => event.profileId)
|
||||
.filter((id): id is string => !!id)
|
||||
)
|
||||
|
||||
for (const profileId of profileIds) {
|
||||
try {
|
||||
await getProfile(profileId)
|
||||
} catch (error) {
|
||||
console.log('Profile not found, create it', profileId)
|
||||
await db.profile.create({
|
||||
data: {
|
||||
project_id: projectId,
|
||||
id: profileId,
|
||||
properties: {},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
await db.event.createMany({
|
||||
data: req.body.map((event) => ({
|
||||
name: event.name,
|
||||
properties: event.properties,
|
||||
createdAt: event.time,
|
||||
project_id: projectId,
|
||||
profile_id: event.profileId,
|
||||
})),
|
||||
})
|
||||
|
||||
res.status(201).json(success())
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user