Add user management and password reset support and bump version to 0.3.7
This commit is contained in:
1 parent
33ca5fcb4a
commit
8b070cf956
6 files changed
+179
-2
No files matched your search
+35
@@ -160,6 +160,37 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "users" && action === "create") {
|
||||
const name = stringOption(args.options.name) ?? maybeName;
|
||||
const phone = stringOption(args.options.phone);
|
||||
if (!name || !phone) throw new Error("Usage: seal-home users create --name <name> --phone <phone> [--dept <deptId>]");
|
||||
const deptId = stringOption(args.options.dept) ?? stringOption(args.options.departmentId);
|
||||
const departmentIds = deptId ? [deptId] : undefined;
|
||||
await runTool("seal_user_create", client, corp, { name, phone, departmentIds }, outputOptions(args.options));
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "users" && action === "reset-password") {
|
||||
const userId = maybeName ?? stringOption(args.options.userId);
|
||||
if (!userId) throw new Error("Usage: seal-home users reset-password <userId>");
|
||||
await runTool("seal_user_reset_password", client, corp, { userId }, outputOptions(args.options));
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "users" && action === "get") {
|
||||
const userId = maybeName ?? stringOption(args.options.userId);
|
||||
if (!userId) throw new Error("Usage: seal-home users get <userId>");
|
||||
await runTool("seal_user_get", client, corp, { userId }, outputOptions(args.options));
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "users" && action === "delete") {
|
||||
const userId = maybeName ?? stringOption(args.options.userId);
|
||||
if (!userId) throw new Error("Usage: seal-home users delete <userId>");
|
||||
await runTool("seal_user_delete", client, corp, { userId }, outputOptions(args.options));
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "context") {
|
||||
await runTool("seal_approval_context_get", client, corp, {
|
||||
documentLimit: numberOption(args.options.documentLimit),
|
||||
@@ -1259,6 +1290,10 @@ Usage:
|
||||
seal-home delegation list [--corp <corpId>]
|
||||
seal-home delegation create [--note <note>] [--corp <corpId>]
|
||||
seal-home delegation revoke <tokenId> [--corp <corpId>]
|
||||
seal-home users create --name <name> --phone <phone> [--dept <deptId>] [--corp <corpId>]
|
||||
seal-home users reset-password <userId> [--corp <corpId>]
|
||||
seal-home users get <userId> [--corp <corpId>]
|
||||
seal-home users delete <userId> [--corp <corpId>]
|
||||
seal-home source config [--corp <corpId>]
|
||||
seal-home tool <toolName> [--corp <corpId>] [--json '{"key":"value"}']
|
||||
seal-home rules count [--corp <corpId>]
|
||||
|
||||
+64
-1
@@ -24,7 +24,13 @@ import {
|
||||
SealDelegationItem,
|
||||
SealDelegationItemSchema,
|
||||
SealDelegationCreateResult,
|
||||
SealDelegationCreateResultSchema
|
||||
SealDelegationCreateResultSchema,
|
||||
SealUserCreateResult,
|
||||
SealUserCreateResultSchema,
|
||||
SealUserResetPasswordResult,
|
||||
SealUserResetPasswordResultSchema,
|
||||
SealUserDetail,
|
||||
SealUserDetailSchema
|
||||
} from "./types.js";
|
||||
|
||||
async function unwrap<T>(response: Response, schema: z.ZodType<T>): Promise<T> {
|
||||
@@ -333,3 +339,60 @@ export async function revokeDelegation(client: KyInstance, tokenId: string): Pro
|
||||
return json.data?.success ?? response.ok;
|
||||
}
|
||||
|
||||
export async function createLocalUser(
|
||||
client: KyInstance,
|
||||
params: { name: string; phone: string; permissions?: string[]; departmentIds?: string[] }
|
||||
): Promise<SealUserCreateResult> {
|
||||
let departmentIds = params.departmentIds && params.departmentIds.length > 0 ? params.departmentIds : undefined;
|
||||
if (!departmentIds) {
|
||||
const rootDepts = (await client.get("api/v1/contacts/departments/root/children").json()) as {
|
||||
data?: { items?: Array<{ _id: string }> };
|
||||
};
|
||||
const firstId = rootDepts.data?.items?.[0]?._id;
|
||||
if (firstId) {
|
||||
departmentIds = [firstId];
|
||||
}
|
||||
}
|
||||
return unwrap(
|
||||
await client.post("api/v1/contacts/local/users", {
|
||||
json: {
|
||||
name: params.name,
|
||||
phone: params.phone,
|
||||
permissions: params.permissions ?? [],
|
||||
departmentIds: departmentIds ?? []
|
||||
}
|
||||
}),
|
||||
SealUserCreateResultSchema
|
||||
);
|
||||
}
|
||||
|
||||
export async function resetUserPassword(
|
||||
client: KyInstance,
|
||||
userId: string
|
||||
): Promise<SealUserResetPasswordResult> {
|
||||
return unwrap(
|
||||
await client.post(`api/v1/contacts/local/users/${userId}/reset-password`, { json: {} }),
|
||||
SealUserResetPasswordResultSchema
|
||||
);
|
||||
}
|
||||
|
||||
export async function getUserDetail(
|
||||
client: KyInstance,
|
||||
userId: string
|
||||
): Promise<SealUserDetail> {
|
||||
return unwrap(
|
||||
await client.get(`api/v1/contacts/users/${userId}`),
|
||||
SealUserDetailSchema
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteLocalUser(
|
||||
client: KyInstance,
|
||||
userId: string
|
||||
): Promise<boolean> {
|
||||
const response = await client.delete(`api/v1/contacts/local/users/${userId}`);
|
||||
const json = (await response.json()) as { data?: { success?: boolean } };
|
||||
return json.data?.success ?? response.ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,6 +66,18 @@ describe("sealTools", () => {
|
||||
expect(revokeTool).toBeDefined();
|
||||
});
|
||||
|
||||
test("includes user management tools", () => {
|
||||
const createTool = sealTools.find((item) => item.name === "seal_user_create");
|
||||
const resetTool = sealTools.find((item) => item.name === "seal_user_reset_password");
|
||||
const getTool = sealTools.find((item) => item.name === "seal_user_get");
|
||||
const deleteTool = sealTools.find((item) => item.name === "seal_user_delete");
|
||||
|
||||
expect(createTool).toBeDefined();
|
||||
expect(resetTool).toBeDefined();
|
||||
expect(getTool).toBeDefined();
|
||||
expect(deleteTool).toBeDefined();
|
||||
});
|
||||
|
||||
test("includes the approval search tool", () => {
|
||||
const tool = sealTools.find((item) => item.name === "seal_approval_search");
|
||||
expect(tool).toBeDefined();
|
||||
|
||||
@@ -54,6 +54,47 @@ export const sealTools = [
|
||||
handler: async (client: KyInstance, params: { tokenId: string }) =>
|
||||
api.revokeDelegation(client, params.tokenId)
|
||||
},
|
||||
{
|
||||
name: "seal_user_create",
|
||||
description: "创建本地 Seal 用户,生成并返回初始随机密码",
|
||||
parameters: z.object({
|
||||
name: z.string().describe("用户姓名"),
|
||||
phone: z.string().describe("手机号码 (例如 13300001111 或 +8613300001111)"),
|
||||
permissions: z.array(z.string()).optional().describe("权限列表,默认为 []"),
|
||||
departmentIds: z.array(z.string()).optional().describe("关联部门 ID 数组")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: { name: string; phone: string; permissions?: string[]; departmentIds?: string[] }
|
||||
) => api.createLocalUser(client, params)
|
||||
},
|
||||
{
|
||||
name: "seal_user_reset_password",
|
||||
description: "重置指定 Seal 用户的默认/随机密码,并返回生成的新密码",
|
||||
parameters: z.object({
|
||||
userId: z.string().describe("用户 ID (_id)")
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { userId: string }) =>
|
||||
api.resetUserPassword(client, params.userId)
|
||||
},
|
||||
{
|
||||
name: "seal_user_get",
|
||||
description: "获取指定 Seal 用户的详细信息",
|
||||
parameters: z.object({
|
||||
userId: z.string().describe("用户 ID (_id)")
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { userId: string }) =>
|
||||
api.getUserDetail(client, params.userId)
|
||||
},
|
||||
{
|
||||
name: "seal_user_delete",
|
||||
description: "删除指定 Seal 本地用户",
|
||||
parameters: z.object({
|
||||
userId: z.string().describe("用户 ID (_id)")
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { userId: string }) =>
|
||||
api.deleteLocalUser(client, params.userId)
|
||||
},
|
||||
{
|
||||
name: "seal_approval_rules_list",
|
||||
description: "列出 Seal 当前草稿审批规则;支持 countOnly、fields、limit 以避免返回规则正文",
|
||||
|
||||
@@ -234,6 +234,28 @@ export const SealDelegationCreateResultSchema = z.object({
|
||||
link: z.string()
|
||||
}).passthrough();
|
||||
|
||||
export const SealUserCreateResultSchema = z.object({
|
||||
userId: z.string(),
|
||||
password: z.string()
|
||||
}).passthrough();
|
||||
|
||||
export const SealUserResetPasswordResultSchema = z.object({
|
||||
password: z.string()
|
||||
}).passthrough();
|
||||
|
||||
export const SealUserDetailSchema = z.object({
|
||||
_id: z.string(),
|
||||
tenantId: z.string().optional(),
|
||||
name: z.string(),
|
||||
mobile: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
departmentIds: z.array(z.string()).default([]),
|
||||
status: z.string().optional(),
|
||||
permissions: z.array(z.string()).default([]),
|
||||
createdAt: z.string().optional(),
|
||||
updatedAt: z.string().optional()
|
||||
}).passthrough();
|
||||
|
||||
export type MeData = z.infer<typeof MeDataSchema>;
|
||||
export type SealSessionData = z.infer<typeof SealSessionDataSchema>;
|
||||
export type ApprovalRule = z.infer<typeof ApprovalRuleSchema>;
|
||||
@@ -248,4 +270,8 @@ export type ApprovalRun = z.infer<typeof ApprovalRunSchema>;
|
||||
export type ApprovalRunListData = z.infer<typeof ApprovalRunListDataSchema>;
|
||||
export type SealDelegationItem = z.infer<typeof SealDelegationItemSchema>;
|
||||
export type SealDelegationCreateResult = z.infer<typeof SealDelegationCreateResultSchema>;
|
||||
export type SealUserCreateResult = z.infer<typeof SealUserCreateResultSchema>;
|
||||
export type SealUserResetPasswordResult = z.infer<typeof SealUserResetPasswordResultSchema>;
|
||||
export type SealUserDetail = z.infer<typeof SealUserDetailSchema>;
|
||||
|
||||
|
||||
Reference in new issue
Block a user