init
This commit is contained in:
29
apps/backend/src/services/event.ts
Normal file
29
apps/backend/src/services/event.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { EventPayload } from "@mixan/types";
|
||||
import { db } from "../db";
|
||||
|
||||
export function getEvents(projectId: string) {
|
||||
return db.event.findMany({
|
||||
where: {
|
||||
project_id: projectId,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function getProfileIdFromEvents(projectId: string, events: EventPayload[]) {
|
||||
const event = events.find(item => !!item.externalId)
|
||||
if(event?.externalId) {
|
||||
return db.profile.findUnique({
|
||||
where: {
|
||||
project_id_external_id: {
|
||||
project_id: projectId,
|
||||
external_id: event.externalId,
|
||||
}
|
||||
}
|
||||
}).then((res) => {
|
||||
return res?.id || null
|
||||
}).catch(() => {
|
||||
return null
|
||||
})
|
||||
}
|
||||
return null
|
||||
}
|
||||
7
apps/backend/src/services/password.ts
Normal file
7
apps/backend/src/services/password.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export async function hashPassword(password: string) {
|
||||
return await Bun.password.hash(password);
|
||||
}
|
||||
|
||||
export async function verifyPassword(password: string,hashedPassword: string) {
|
||||
return await Bun.password.verify(password, hashedPassword);
|
||||
}
|
||||
75
apps/backend/src/services/profile.ts
Normal file
75
apps/backend/src/services/profile.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { EventPayload, ProfilePayload } from "@mixan/types";
|
||||
import { db } from "../db";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export function createProfile(projectId: string, payload: ProfilePayload) {
|
||||
const { id, email, first_name, last_name, avatar, properties } = payload
|
||||
return db.profile.create({
|
||||
data:{
|
||||
external_id: id,
|
||||
email,
|
||||
first_name,
|
||||
last_name,
|
||||
avatar,
|
||||
properties: properties || {},
|
||||
project_id: projectId,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type DbProfile = Exclude<Prisma.PromiseReturnType<typeof getProfileByExternalId>, null>
|
||||
|
||||
export function getProfileByExternalId(projectId: string, externalId: string) {
|
||||
return db.profile.findUnique({
|
||||
where: {
|
||||
project_id_external_id: {
|
||||
project_id: projectId,
|
||||
external_id: externalId,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function getProfiles(projectId: string) {
|
||||
return db.profile.findMany({
|
||||
where: {
|
||||
project_id: projectId,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateProfile(projectId: string, profileId: string, payload: Omit<ProfilePayload, 'id'>, oldProfile: DbProfile) {
|
||||
const { email, first_name, last_name, avatar, properties } = payload
|
||||
return db.profile.update({
|
||||
where: {
|
||||
project_id_external_id: {
|
||||
project_id: projectId,
|
||||
external_id: profileId,
|
||||
}
|
||||
},
|
||||
data: {
|
||||
email,
|
||||
first_name,
|
||||
last_name,
|
||||
avatar,
|
||||
properties: {
|
||||
...(typeof oldProfile.properties === 'object' ? oldProfile.properties || {} : {}),
|
||||
...(properties || {}),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function getInternalProfileId(profileId?: string | null) {
|
||||
if(!profileId) {
|
||||
return null
|
||||
}
|
||||
|
||||
const profile = await db.profile.findFirst({
|
||||
where: {
|
||||
external_id: profileId,
|
||||
}
|
||||
})
|
||||
|
||||
return profile?.id || null
|
||||
}
|
||||
Reference in New Issue
Block a user