Skip to content

Commit 2bed792

Browse files
authored
Feat/add member (#16)
1 parent fb7a448 commit 2bed792

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

packages/client/src/routes/orgs/[orgId]/settings/+page.svelte

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { api, type Id } from "@packages/convex";
3-
import { useQuery } from "convex-svelte";
3+
import { useConvexClient, useQuery } from "convex-svelte";
44
import { page } from "$app/stores";
55
import { useMutation } from "~/lib/useMutation.svelte.ts";
66
@@ -14,6 +14,7 @@
1414
}));
1515
const updateOrganization = useMutation(api.organizations.update);
1616
const removeMember = useMutation(api.organizations.removeMember);
17+
const convex = useConvexClient();
1718
1819
let isEditing = $state(false);
1920
let editForm = $state({
@@ -55,6 +56,41 @@
5556
}
5657
}
5758
}
59+
60+
async function addMember() {
61+
let email = prompt("追加するメンバーのメールアドレスを入力してください");
62+
if (!email?.trim()) return;
63+
64+
if (members.data) {
65+
for (const m of members.data) {
66+
if (m.user?.email === email) {
67+
alert("そのメンバーはもう存在します");
68+
return;
69+
}
70+
}
71+
}
72+
const users = await convex.query(api.users.getUsersByEmail, { email });
73+
if (!users.length) {
74+
alert("ユーザーが見つかりませんでした");
75+
return;
76+
}
77+
if (users.length > 1) {
78+
alert(
79+
"同じメールアドレスで登録されている人物が複数確認されました。開発者に報告してください。",
80+
);
81+
return;
82+
}
83+
let message = `以下のユーザーが見つかりました\n${users[0]?.name}\n組織に追加しますか?`;
84+
85+
const answer = confirm(message);
86+
if (answer && users[0]) {
87+
convex.mutation(api.organizations.addMember, {
88+
organizationId: organizationId,
89+
userId: users[0]._id as Id<"users">,
90+
permission: "member",
91+
});
92+
}
93+
}
5894
</script>
5995

6096
<div class="container mx-auto p-6">
@@ -143,7 +179,9 @@
143179
<div class="mb-4 flex items-center justify-between">
144180
<h2 class="card-title">メンバー</h2>
145181
{#if organization.data?.permission === "admin"}
146-
<button class="btn btn-primary btn-sm"> メンバーを追加 </button>
182+
<button class="btn btn-primary btn-sm" onclick={addMember}>
183+
メンバーを追加
184+
</button>
147185
{/if}
148186
</div>
149187

packages/convex/src/convex/users.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ export const getUserNicknames = query({
6464
return userNicknames;
6565
},
6666
});
67+
68+
export const getUsersByEmail = query({
69+
args: {
70+
email: v.string(),
71+
},
72+
handler: async (ctx, args) => {
73+
return await ctx.db
74+
.query("users")
75+
.filter((q) => q.eq(q.field("email"), args.email))
76+
.collect();
77+
},
78+
});

0 commit comments

Comments
 (0)