create profile if not exists
This commit is contained in:
@@ -14,9 +14,11 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@mixan/types": "workspace:*",
|
"@mixan/types": "workspace:*",
|
||||||
"@prisma/client": "^5.4.2",
|
"@prisma/client": "^5.4.2",
|
||||||
|
"@types/ramda": "^0.29.6",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"prisma": "^5.4.2",
|
"prisma": "^5.4.2",
|
||||||
|
"ramda": "^0.29.1",
|
||||||
"random-animal-name": "^0.1.1",
|
"random-animal-name": "^0.1.1",
|
||||||
"uuid": "^9.0.1"
|
"uuid": "^9.0.1"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import {NextFunction, Response, Router} from 'express'
|
import { NextFunction, Response, Router } from 'express'
|
||||||
import { db } from '../db';
|
import { db } from '../db'
|
||||||
import { MixanRequest } from '../types/express';
|
import { MixanRequest } from '../types/express'
|
||||||
import { EventPayload } from '@mixan/types';
|
import { EventPayload } from '@mixan/types'
|
||||||
import { getEvents } from '../services/event';
|
import { getEvents } from '../services/event'
|
||||||
import { success } from '../responses/success';
|
import { success } from '../responses/success'
|
||||||
|
import { getProfile } from '../services/profile'
|
||||||
const router = Router();
|
import { uniq } from 'ramda'
|
||||||
|
const router = Router()
|
||||||
|
|
||||||
type PostRequest = MixanRequest<Array<EventPayload>>
|
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) => {
|
router.post(
|
||||||
try {
|
'/events',
|
||||||
const projectId = req.client.project_id
|
async (req: PostRequest, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
await db.event.createMany({
|
const projectId = req.client.project_id
|
||||||
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
|
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