✨ Implement API update user
This commit is contained in:
@@ -31,6 +31,8 @@ import { isNumber, isString } from "lodash";
|
||||
import NodeCache from "node-cache";
|
||||
import gr from "../../../global-resolver";
|
||||
import { TYPE as DriveFileType, DriveFile } from "../../../documents/entities/drive-file";
|
||||
import { UpdateUser } from "./types";
|
||||
import { formatUsername } from "../../../../utils/users";
|
||||
|
||||
export class UserServiceImpl {
|
||||
version: "1";
|
||||
@@ -77,14 +79,29 @@ export class UserServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
private async updateExtRepositoryInCaseChangeEmail(user: User, context?: ExecutionContext) {
|
||||
if (user.identity_provider_id === "null") {
|
||||
return;
|
||||
}
|
||||
|
||||
const extUser = await this.extUserRepository.findOne({ user_id: user.id }, {}, context);
|
||||
if (extUser) {
|
||||
await this.extUserRepository.remove(extUser, context);
|
||||
}
|
||||
|
||||
const newExtUser = getExternalUserInstance({
|
||||
service_id: user.identity_provider || "null",
|
||||
external_id: user.identity_provider_id || "null",
|
||||
user_id: user.id,
|
||||
});
|
||||
await this.extUserRepository.save(newExtUser, context);
|
||||
}
|
||||
|
||||
private assignDefaults(user: User) {
|
||||
user.creation_date = !isNumber(user.creation_date) ? Date.now() : user.creation_date;
|
||||
if (user.identity_provider_id && !user.identity_provider) user.identity_provider = "console";
|
||||
if (user.email_canonical) user.email_canonical = user.email_canonical.toLocaleLowerCase();
|
||||
if (user.username_canonical)
|
||||
user.username_canonical = (user.username_canonical || "")
|
||||
.toLocaleLowerCase()
|
||||
.replace(/[^a-z0-9_-]/, "");
|
||||
if (user.username_canonical) user.username_canonical = formatUsername(user.username_canonical);
|
||||
}
|
||||
|
||||
async create(user: User, context?: ExecutionContext): Promise<CreateResult<User>> {
|
||||
@@ -92,8 +109,35 @@ export class UserServiceImpl {
|
||||
return new CreateResult("user", user);
|
||||
}
|
||||
|
||||
update(pk: Partial<User>, item: User, context?: ExecutionContext): Promise<UpdateResult<User>> {
|
||||
throw new Error("Method not implemented.");
|
||||
async update(
|
||||
instance: User,
|
||||
user: UpdateUser,
|
||||
context?: ExecutionContext,
|
||||
): Promise<UpdateResult<User>> {
|
||||
let isChangeEmail = false;
|
||||
if (instance.email_canonical !== user.email) {
|
||||
isChangeEmail = true;
|
||||
}
|
||||
|
||||
instance.email_canonical = user.email || instance.email_canonical;
|
||||
instance.first_name = user.first_name || instance.first_name;
|
||||
instance.last_name = user.last_name || instance.last_name;
|
||||
instance.picture = user.picture || instance.picture;
|
||||
instance.creation_date = !isNumber(instance.creation_date)
|
||||
? Date.now()
|
||||
: instance.creation_date;
|
||||
if (isChangeEmail) {
|
||||
instance.username_canonical = formatUsername(user.email);
|
||||
instance.identity_provider_id =
|
||||
instance.identity_provider_id !== "null" ? user.email : "null";
|
||||
}
|
||||
|
||||
await this.repository.save(instance, context);
|
||||
if (isChangeEmail) {
|
||||
await this.updateExtRepositoryInCaseChangeEmail(instance, context);
|
||||
}
|
||||
|
||||
return new UpdateResult("user", instance);
|
||||
}
|
||||
|
||||
async save(user: User, context?: ExecutionContext): Promise<SaveResult<User>> {
|
||||
|
||||
@@ -8,3 +8,10 @@ export type SearchUserOptions = {
|
||||
workspaceId?: string;
|
||||
channelId?: string;
|
||||
};
|
||||
|
||||
export type UpdateUser = {
|
||||
email: string;
|
||||
picture: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
};
|
||||
|
||||
@@ -37,6 +37,7 @@ import { formatUser } from "../../../utils/users";
|
||||
import gr from "../../global-resolver";
|
||||
import config from "config";
|
||||
import { getLogger } from "../../../core/platform/framework";
|
||||
import { UpdateUser } from "../services/users/types";
|
||||
|
||||
export class UsersCrudController
|
||||
implements
|
||||
@@ -366,6 +367,35 @@ export class UsersCrudController
|
||||
used: quota,
|
||||
} as UserQuota;
|
||||
}
|
||||
|
||||
async update(
|
||||
request: FastifyRequest<{ Body: UpdateUser; Params: UserParameters }>,
|
||||
reply: FastifyReply,
|
||||
): Promise<ResourceCreateResponse<UserObject>> {
|
||||
const context = getExecutionContext(request);
|
||||
|
||||
const id = request.params.id;
|
||||
const user = await gr.services.users.get({ id });
|
||||
if (!user) {
|
||||
reply.notFound(`User ${id} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
const body = { ...request.body };
|
||||
if (user.email_canonical !== body.email) {
|
||||
const userByEmail = await gr.services.users.getByEmail(body.email, context);
|
||||
if (userByEmail) {
|
||||
reply.conflict(`Email ${body.email} is existed`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await gr.services.users.update(user, body, context);
|
||||
|
||||
return {
|
||||
resource: await formatUser(user),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function getExecutionContext(request: FastifyRequest): ExecutionContext {
|
||||
|
||||
@@ -143,6 +143,13 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
|
||||
handler: usersController.recent.bind(usersController),
|
||||
});
|
||||
|
||||
fastify.route({
|
||||
method: "PUT",
|
||||
url: `${usersUrl}/:id`,
|
||||
preValidation: [fastify.authenticateOptional],
|
||||
handler: usersController.update.bind(usersController),
|
||||
});
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
|
||||
@@ -87,3 +87,7 @@ export async function formatUser(
|
||||
|
||||
return resUser;
|
||||
}
|
||||
|
||||
export function formatUsername(value: string) {
|
||||
return (value?.replace(/[^a-z0-9_-]/, "") || "").toLocaleLowerCase();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user