Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions packages/client/src/routes/orgs/[orgId]/settings/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { api, type Id } from "@packages/convex";
import { useQuery } from "convex-svelte";
import { useConvexClient, useQuery } from "convex-svelte";
import { page } from "$app/stores";
import { useMutation } from "~/lib/useMutation.svelte.ts";

Expand All @@ -14,6 +14,7 @@
}));
const updateOrganization = useMutation(api.organizations.update);
const removeMember = useMutation(api.organizations.removeMember);
const convex = useConvexClient();

let isEditing = $state(false);
let editForm = $state({
Expand Down Expand Up @@ -55,6 +56,41 @@
}
}
}

async function addMember() {
let email = prompt("追加するメンバーのメールアドレスを入力してください");
if (!email?.trim()) return;

if (members.data) {
for (const m of members.data) {
if (m.user?.email === email) {
alert("そのメンバーはもう存在します");
return;
}
}
}
const users = await convex.query(api.users.getUsersByEmail, { email });
if (!users.length) {
alert("ユーザーが見つかりませんでした");
return;
}
if (users.length > 1) {
alert(
"同じメールアドレスで登録されている人物が複数確認されました。開発者に報告してください。",
);
return;
}
let message = `以下のユーザーが見つかりました\n${users[0]?.name}\n組織に追加しますか?`;

const answer = confirm(message);
if (answer && users[0]) {
convex.mutation(api.organizations.addMember, {
organizationId: organizationId,
userId: users[0]._id as Id<"users">,
permission: "member",
});
}
}
</script>

<div class="container mx-auto p-6">
Expand Down Expand Up @@ -143,7 +179,9 @@
<div class="mb-4 flex items-center justify-between">
<h2 class="card-title">メンバー</h2>
{#if organization.data?.permission === "admin"}
<button class="btn btn-primary btn-sm"> メンバーを追加 </button>
<button class="btn btn-primary btn-sm" onclick={addMember}>
メンバーを追加
</button>
{/if}
</div>

Expand Down
12 changes: 12 additions & 0 deletions packages/convex/src/convex/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ export const getUserNicknames = query({
return userNicknames;
},
});

export const getUsersByEmail = query({
args: {
email: v.string(),
},
handler: async (ctx, args) => {
return await ctx.db
.query("users")
.filter((q) => q.eq(q.field("email"), args.email))
.collect();
},
});