Add "Send email" button for Support users

This commit is contained in:
Emi Matchu 2021-03-23 16:50:09 -07:00
parent 0e8e50b054
commit 790c231b5d
3 changed files with 58 additions and 2 deletions

View file

@ -555,6 +555,24 @@ function UserSupportMenu({ children, user }) {
const { supportSecret } = useSupport(); const { supportSecret } = useSupport();
const toast = useToast(); const toast = useToast();
const { loading, error, data } = useQuery(
gql`
query UserSupportMenu($userId: ID!, $supportSecret: String!) {
user(id: $userId) {
id
emailForSupportUsers(supportSecret: $supportSecret)
}
}
`,
{
variables: {
userId: user.id,
supportSecret,
onError: (e) => console.error(e),
},
}
);
const [sendEditUsernameMutation] = useMutation( const [sendEditUsernameMutation] = useMutation(
gql` gql`
mutation UserSupportMenuRename( mutation UserSupportMenuRename(
@ -615,6 +633,23 @@ function UserSupportMenu({ children, user }) {
<Portal> <Portal>
<MenuList> <MenuList>
<MenuItem onClick={editUsername}>Edit username</MenuItem> <MenuItem onClick={editUsername}>Edit username</MenuItem>
<MenuItem
as="a"
href={
data?.user?.emailForSupportUsers
? `mailto:${data.user.emailForSupportUsers}`
: undefined
}
isDisabled={data?.user?.emailForSupportUsers == null}
sx={{
'&[aria-disabled="true"]': {
cursor: loading ? "wait !important" : "not-allowed",
},
}}
>
Send email
{error && <> (Error: {error.message})</>}
</MenuItem>
</MenuList> </MenuList>
</Portal> </Portal>
</Menu> </Menu>

View file

@ -878,4 +878,4 @@ function assertSupportSecretOrThrow(supportSecret) {
} }
} }
module.exports = { typeDefs, resolvers }; module.exports = { typeDefs, resolvers, assertSupportSecretOrThrow };

View file

@ -1,5 +1,7 @@
import { gql } from "apollo-server"; import { gql } from "apollo-server";
import { assertSupportSecretOrThrow } from "./MutationsForSupport";
const typeDefs = gql` const typeDefs = gql`
type User { type User {
id: ID! id: ID!
@ -23,6 +25,9 @@ const typeDefs = gql`
# This user's outfits. Returns an empty list if the current user is not # This user's outfits. Returns an empty list if the current user is not
# authorized to see them. # authorized to see them.
outfits: [Outfit!]! outfits: [Outfit!]!
# This user's email address. Requires the correct supportSecret to view.
emailForSupportUsers(supportSecret: String!): String!
} }
extend type Query { extend type Query {
@ -218,7 +223,6 @@ const resolvers = {
isDefaultList: true, isDefaultList: true,
userId: id, userId: id,
ownsOrWantsItems: "WANTS", ownsOrWantsItems: "WANTS",
isDefaultList: true,
items: defaultListWantedHangers.map((h) => ({ id: h.itemId })), items: defaultListWantedHangers.map((h) => ({ id: h.itemId })),
}); });
} }
@ -239,6 +243,23 @@ const resolvers = {
const outfits = await userOutfitsLoader.load(id); const outfits = await userOutfitsLoader.load(id);
return outfits.map((outfit) => ({ id: outfit.id })); return outfits.map((outfit) => ({ id: outfit.id }));
}, },
emailForSupportUsers: async ({ id }, { supportSecret }, { db }) => {
assertSupportSecretOrThrow(supportSecret);
const [rows] = await db.query(
`
SELECT email FROM openneo_id.users
WHERE id = (SELECT remote_id FROM users WHERE id = ?)
`,
[id]
);
if (rows.length === 0) {
throw new Error(`could not find user ${id} in openneo_id database`);
}
return rows[0].email;
},
}, },
Query: { Query: {