add nextjs and migrated api to next api
This commit is contained in:
33
apps/web/src/pages/api/sdk/profiles/[profileId]/decrement.ts
Normal file
33
apps/web/src/pages/api/sdk/profiles/[profileId]/decrement.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { validateSdkRequest } from "@/server/auth";
|
||||
import { db } from "@/server/db";
|
||||
import { createError, handleError } from "@/server/exceptions";
|
||||
import { tickProfileProperty } from "@/services/profile.service";
|
||||
import { ProfileIncrementPayload, ProfilePayload } from "@mixan/types";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
interface Request extends NextApiRequest {
|
||||
body: ProfileIncrementPayload;
|
||||
}
|
||||
|
||||
export default async function handler(req: Request, res: NextApiResponse) {
|
||||
if (req.method !== "PUT") {
|
||||
return handleError(res, createError(405, "Method not allowed"));
|
||||
}
|
||||
|
||||
try {
|
||||
// Check client id & secret
|
||||
await validateSdkRequest(req)
|
||||
|
||||
const profileId = req.query.profileId as string;
|
||||
|
||||
await tickProfileProperty({
|
||||
name: req.body.name,
|
||||
tick: -Math.abs(req.body.value),
|
||||
profileId,
|
||||
});
|
||||
|
||||
res.status(200).end();
|
||||
} catch (error) {
|
||||
handleError(res, error);
|
||||
}
|
||||
}
|
||||
33
apps/web/src/pages/api/sdk/profiles/[profileId]/increment.ts
Normal file
33
apps/web/src/pages/api/sdk/profiles/[profileId]/increment.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { validateSdkRequest } from "@/server/auth";
|
||||
import { db } from "@/server/db";
|
||||
import { createError, handleError } from "@/server/exceptions";
|
||||
import { tickProfileProperty } from "@/services/profile.service";
|
||||
import { ProfileIncrementPayload, ProfilePayload } from "@mixan/types";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
interface Request extends NextApiRequest {
|
||||
body: ProfileIncrementPayload;
|
||||
}
|
||||
|
||||
export default async function handler(req: Request, res: NextApiResponse) {
|
||||
if (req.method !== "PUT") {
|
||||
return handleError(res, createError(405, "Method not allowed"));
|
||||
}
|
||||
|
||||
try {
|
||||
// Check client id & secret
|
||||
await validateSdkRequest(req)
|
||||
|
||||
const profileId = req.query.profileId as string;
|
||||
|
||||
await tickProfileProperty({
|
||||
name: req.body.name,
|
||||
tick: req.body.value,
|
||||
profileId,
|
||||
});
|
||||
|
||||
res.status(200).end();
|
||||
} catch (error) {
|
||||
handleError(res, error);
|
||||
}
|
||||
}
|
||||
48
apps/web/src/pages/api/sdk/profiles/[profileId]/index.ts
Normal file
48
apps/web/src/pages/api/sdk/profiles/[profileId]/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { validateSdkRequest } from "@/server/auth";
|
||||
import { db } from "@/server/db";
|
||||
import { createError, handleError } from "@/server/exceptions";
|
||||
import { getProfile } from "@/services/profile.service";
|
||||
import { ProfilePayload } from "@mixan/types";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
interface Request extends NextApiRequest {
|
||||
body: ProfilePayload;
|
||||
}
|
||||
|
||||
export default async function handler(req: Request, res: NextApiResponse) {
|
||||
if (req.method !== "PUT" && req.method !== "POST") {
|
||||
return handleError(res, createError(405, "Method not allowed"));
|
||||
}
|
||||
|
||||
try {
|
||||
// Check client id & secret
|
||||
await validateSdkRequest(req)
|
||||
|
||||
const profileId = req.query.profileId as string;
|
||||
const profile = await getProfile(profileId)
|
||||
|
||||
const { body } = req;
|
||||
await db.profile.update({
|
||||
where: {
|
||||
id: profileId,
|
||||
},
|
||||
data: {
|
||||
external_id: body.id,
|
||||
email: body.email,
|
||||
first_name: body.first_name,
|
||||
last_name: body.last_name,
|
||||
avatar: body.avatar,
|
||||
properties: {
|
||||
...(typeof profile.properties === "object"
|
||||
? profile.properties || {}
|
||||
: {}),
|
||||
...(body.properties || {}),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).end();
|
||||
} catch (error) {
|
||||
handleError(res, error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user