-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
538 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Context } from 'koa'; | ||
import { z } from 'zod'; | ||
|
||
import { | ||
Body, | ||
Controller, | ||
Ctx, | ||
Get, | ||
Post, | ||
Query, | ||
ResponseBody, | ||
UseMiddlewares, | ||
} from '@/common/http'; | ||
import { ParseIntPipe, ZodValidationPipe } from '@/common/pipe'; | ||
import { adminOnly, auth } from '@/middleware'; | ||
import { userService } from '@/user/services/user'; | ||
import { MergeUserTaskResponse } from '@/response/merge-user-task'; | ||
import { MergeUserTask } from '@/model/MergeUserTask'; | ||
|
||
const MergeUserSchema = z.object({ | ||
sourceUserId: z.string(), | ||
targetUserId: z.string(), | ||
}); | ||
|
||
@Controller('merge-user-tasks') | ||
@UseMiddlewares(auth, adminOnly) | ||
export class MergeUserTaskController { | ||
@Post() | ||
@ResponseBody(MergeUserTaskResponse) | ||
async createTask( | ||
@Body(new ZodValidationPipe(MergeUserSchema)) | ||
data: z.infer<typeof MergeUserSchema> | ||
) { | ||
return userService.mergeUser(data.sourceUserId, data.targetUserId); | ||
} | ||
|
||
@Get() | ||
@ResponseBody(MergeUserTaskResponse) | ||
async getTasks( | ||
@Ctx() | ||
ctx: Context, | ||
@Query('page', new ParseIntPipe({ min: 1 })) | ||
page: number, | ||
@Query('pageSize', new ParseIntPipe({ min: 1, max: 100 })) | ||
pageSize: number | ||
) { | ||
const [tasks, totalCount] = await MergeUserTask.queryBuilder() | ||
.preload('sourceUser') | ||
.preload('targetUser') | ||
.paginate(page, pageSize) | ||
.findAndCount({ useMasterKey: true }); | ||
ctx.set('X-Total-Count', totalCount.toString()); | ||
return tasks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { field, Model, pointerId, pointTo } from '@/orm'; | ||
import { User } from './User'; | ||
|
||
export class MergeUserTask extends Model { | ||
@pointerId(() => User) | ||
sourceUserId!: string; | ||
|
||
@pointTo(() => User) | ||
sourceUser?: User; | ||
|
||
@pointerId(() => User) | ||
targetUserId!: string; | ||
|
||
@pointTo(() => User) | ||
targetUser?: User; | ||
|
||
@field() | ||
mergingData!: { | ||
authData?: Record<string, any>; | ||
email?: string; | ||
}; | ||
|
||
@field() | ||
status!: string; | ||
|
||
@field() | ||
completedAt?: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { MergeUserTask } from '@/model/MergeUserTask'; | ||
import { UserResponse } from './user'; | ||
|
||
export class MergeUserTaskResponse { | ||
constructor(private task: MergeUserTask) {} | ||
|
||
toJSON() { | ||
return { | ||
id: this.task.id, | ||
sourceUserId: this.task.sourceUserId, | ||
sourceUser: this.task.sourceUser && new UserResponse(this.task.sourceUser).toJSON(), | ||
targetUser: this.task.targetUser && new UserResponse(this.task.targetUser).toJSON(), | ||
targetUserId: this.task.targetUserId, | ||
status: this.task.status, | ||
completedAt: this.task.completedAt?.toISOString(), | ||
createdAt: this.task.createdAt.toISOString(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.