From 88cf87abb223385383f9366eb3176ccae554204e Mon Sep 17 00:00:00 2001 From: jingyang <3161362058@qq.com> Date: Fri, 14 Jun 2024 17:58:05 +0800 Subject: [PATCH 1/2] feat: support workorder migrate --- .../workorder/src/pages/api/v1/migrate.ts | 54 +++++++++++++++++++ .../workorder/src/services/db/workorder.ts | 32 +++++++++++ 2 files changed, 86 insertions(+) create mode 100644 frontend/providers/workorder/src/pages/api/v1/migrate.ts diff --git a/frontend/providers/workorder/src/pages/api/v1/migrate.ts b/frontend/providers/workorder/src/pages/api/v1/migrate.ts new file mode 100644 index 00000000000..24331b25465 --- /dev/null +++ b/frontend/providers/workorder/src/pages/api/v1/migrate.ts @@ -0,0 +1,54 @@ +import { NextApiRequest, NextApiResponse } from 'next'; +import { migrateWorkOrders } from '@/services/db/workorder'; +import { verify } from 'jsonwebtoken'; +import { jsonRes } from '@/services/backend/response'; +import { getUserById, updateUser } from '@/services/db/user'; + +const desktopJwtSecret = (process.env.JWT_SECRET_DESKTOP_TO_APP as string) || '123456789'; + +const verifyToken = (token: string) => { + try { + return verify(token, desktopJwtSecret) as { userUid: string; mergeUserUid: string }; + } catch (error) { + throw new Error('Token verification failed'); + } +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const token = req.body.token; + if (!token) { + return jsonRes(res, { code: 400, data: 'Invalid parameters' }); + } + + const { userUid, mergeUserUid } = verifyToken(token); + + if (!mergeUserUid || !userUid) { + return jsonRes(res, { code: 400, data: 'Invalid user identifiers' }); + } + + const mergeUser = await getUserById(mergeUserUid); + const targetUser = await getUserById(userUid); + + if (!mergeUser) { + return jsonRes(res, { code: 200, data: 'Merge user not found, nothing to do' }); + } + + const migrationResult = await migrateWorkOrders({ mergeUserUid, userUid }); + if (!migrationResult.success) { + return jsonRes(res, { code: 500, data: migrationResult.message }); + } + + if (!targetUser) { + const updateResult = await updateUser(mergeUserUid, { userId: userUid }); + if (updateResult.matchedCount === 0) { + return jsonRes(res, { code: 500, data: 'Failed to update user' }); + } + } + + return jsonRes(res, { code: 200, data: 'Success' }); + } catch (error) { + console.log('Error in migrate handler:', error); + return jsonRes(res, { code: 500, data: error }); + } +} diff --git a/frontend/providers/workorder/src/services/db/workorder.ts b/frontend/providers/workorder/src/services/db/workorder.ts index 6cffb1e1423..250d4768252 100644 --- a/frontend/providers/workorder/src/services/db/workorder.ts +++ b/frontend/providers/workorder/src/services/db/workorder.ts @@ -1,6 +1,7 @@ import { WorkOrderDB, WorkOrderDialog, WorkOrderStatus, WorkOrderType } from '@/types/workorder'; import { connectToDatabase } from './mongodb'; import { getUserById } from './user'; +import { ClientSession } from 'mongodb'; async function connectOrderCollection() { const client = await connectToDatabase(); @@ -154,3 +155,34 @@ export async function deleteOrder({ orderId, userId }: { orderId: string; userId const result = await collection.updateOne(filter, update); return result; } + +export async function migrateWorkOrders({ + mergeUserUid, + userUid +}: { + mergeUserUid: string; + userUid: string; +}): Promise<{ success: boolean; message: string }> { + try { + const collection = await connectOrderCollection(); + + const mergeUserOrders = await collection.find({ userId: mergeUserUid }).toArray(); + if (mergeUserOrders.length === 0) { + return { success: true, message: 'No work orders found for mergeUserUid' }; + } + + const updateResult = await collection.updateMany( + { userId: mergeUserUid }, + { $set: { userId: userUid } } + ); + + if (updateResult.modifiedCount === 0) { + return { success: false, message: 'Failed to migrate work orders' }; + } + + return { success: true, message: 'Work orders migrated successfully' }; + } catch (error) { + console.log(error); + return { success: false, message: 'Error migrating work orders' }; + } +} From 2263bd11f95a64359fcd0e5273b0b11e48f0e1bc Mon Sep 17 00:00:00 2001 From: jingyang <3161362058@qq.com> Date: Fri, 14 Jun 2024 18:12:07 +0800 Subject: [PATCH 2/2] fix --- frontend/providers/workorder/src/pages/api/v1/migrate.ts | 3 +-- frontend/providers/workorder/src/services/backend/auth.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/frontend/providers/workorder/src/pages/api/v1/migrate.ts b/frontend/providers/workorder/src/pages/api/v1/migrate.ts index 24331b25465..cb295c98fc9 100644 --- a/frontend/providers/workorder/src/pages/api/v1/migrate.ts +++ b/frontend/providers/workorder/src/pages/api/v1/migrate.ts @@ -3,8 +3,7 @@ import { migrateWorkOrders } from '@/services/db/workorder'; import { verify } from 'jsonwebtoken'; import { jsonRes } from '@/services/backend/response'; import { getUserById, updateUser } from '@/services/db/user'; - -const desktopJwtSecret = (process.env.JWT_SECRET_DESKTOP_TO_APP as string) || '123456789'; +import { desktopJwtSecret } from '@/services/backend/auth'; const verifyToken = (token: string) => { try { diff --git a/frontend/providers/workorder/src/services/backend/auth.ts b/frontend/providers/workorder/src/services/backend/auth.ts index a110acd7675..c2ab68bf9ce 100644 --- a/frontend/providers/workorder/src/services/backend/auth.ts +++ b/frontend/providers/workorder/src/services/backend/auth.ts @@ -3,7 +3,7 @@ import { verify, sign } from 'jsonwebtoken'; import type { NextApiRequest } from 'next'; import { ERROR_ENUM } from '../error'; -const desktopJwtSecret = (process.env.JWT_SECRET_DESKTOP_TO_APP as string) || '123456789'; +export const desktopJwtSecret = (process.env.JWT_SECRET_DESKTOP_TO_APP as string) || '123456789'; const appJwtSecret = (process.env.JWT_SECRET_SELF as string) || '123456789'; export const verifyAccessToken = async (req: NextApiRequest) => {