From 3a4b6b2fc3e479324fb372c44509fd7a9af1ef98 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Wed, 16 Dec 2020 17:12:23 -0800 Subject: [PATCH] Added employee email validation IO-421 --- bodyshop_translations.babel | 26 ++++++++++++++ .../bill-form/bill-form.component.jsx | 1 + .../shop-employees-form.component.jsx | 35 ++++++++++++++++++- client/src/graphql/employees.queries.js | 8 +++++ client/src/translations/en_us/common.json | 3 ++ client/src/translations/es/common.json | 3 ++ client/src/translations/fr/common.json | 3 ++ 7 files changed, 78 insertions(+), 1 deletion(-) diff --git a/bodyshop_translations.babel b/bodyshop_translations.babel index f81d5783a..1233b3fb8 100644 --- a/bodyshop_translations.babel +++ b/bodyshop_translations.babel @@ -5955,6 +5955,32 @@ + + validation + + + useremailmustexist + false + + + + + + en-US + false + + + es-MX + false + + + fr-CA + false + + + + + diff --git a/client/src/components/bill-form/bill-form.component.jsx b/client/src/components/bill-form/bill-form.component.jsx index edb7c5ba8..7864d953b 100644 --- a/client/src/components/bill-form/bill-form.component.jsx +++ b/client/src/components/bill-form/bill-form.component.jsx @@ -24,6 +24,7 @@ import LayoutFormRow from "../layout-form-row/layout-form-row.component"; import VendorSearchSelect from "../vendor-search-select/vendor-search-select.component"; import BillFormLines from "./bill-form.lines.component"; import { CalculateBillTotal } from "./bill-form.totals.utility"; + const mapStateToProps = createStructuredSelector({ bodyshop: selectBodyshop, }); diff --git a/client/src/components/shop-employees/shop-employees-form.component.jsx b/client/src/components/shop-employees/shop-employees-form.component.jsx index 47bcf538d..1d09f5ff4 100644 --- a/client/src/components/shop-employees/shop-employees-form.component.jsx +++ b/client/src/components/shop-employees/shop-employees-form.component.jsx @@ -1,12 +1,15 @@ import { Button, Form, Input, InputNumber, Select, Switch } from "antd"; import moment from "moment"; import React, { useEffect } from "react"; +import { useApolloClient } from "react-apollo"; import { useTranslation } from "react-i18next"; import { connect } from "react-redux"; import { createStructuredSelector } from "reselect"; +import { QUERY_USERS_BY_EMAIL } from "../../graphql/employees.queries"; import { selectBodyshop } from "../../redux/user/user.selectors"; import FormDatePicker from "../form-date-picker/form-date-picker.component"; import CurrencyInput from "../form-items-formatted/currency-form-item.component"; + const mapStateToProps = createStructuredSelector({ bodyshop: selectBodyshop, }); @@ -21,6 +24,7 @@ export function ShopEmployeesFormComponent({ handleFinish, }) { const { t } = useTranslation(); + const client = useApolloClient(); useEffect(() => { if (selectedEmployee) form.resetFields(); }, [selectedEmployee, form]); @@ -159,7 +163,36 @@ export function ShopEmployeesFormComponent({ > - + ({ + async validator(rule, value) { + const user_email = getFieldValue("user_email"); + + if (user_email && value) { + const response = await client.query({ + query: QUERY_USERS_BY_EMAIL, + variables: { + email: user_email, + }, + }); + + if (response.data.users.length === 1) { + return Promise.resolve(); + } + return Promise.reject( + t("bodyshop.validation.useremailmustexist") + ); + } else { + return Promise.resolve(); + } + }, + }), + ]} + > diff --git a/client/src/graphql/employees.queries.js b/client/src/graphql/employees.queries.js index cd9aebbdc..658a86810 100644 --- a/client/src/graphql/employees.queries.js +++ b/client/src/graphql/employees.queries.js @@ -59,3 +59,11 @@ export const DELETE_EMPLOYEE = gql` } } `; + +export const QUERY_USERS_BY_EMAIL = gql` + query QUERY_USERS_BY_EMAIL($email: String!) { + users(where: { email: { _ilike: $email } }) { + email + } + } +`; diff --git a/client/src/translations/en_us/common.json b/client/src/translations/en_us/common.json index cb4161015..05708155c 100644 --- a/client/src/translations/en_us/common.json +++ b/client/src/translations/en_us/common.json @@ -383,6 +383,9 @@ }, "successes": { "save": "Bodyshop saved successfully. " + }, + "validation": { + "useremailmustexist": "This email is not a valid user." } }, "checklist": { diff --git a/client/src/translations/es/common.json b/client/src/translations/es/common.json index 636b031d6..fc6ad1df3 100644 --- a/client/src/translations/es/common.json +++ b/client/src/translations/es/common.json @@ -383,6 +383,9 @@ }, "successes": { "save": "" + }, + "validation": { + "useremailmustexist": "" } }, "checklist": { diff --git a/client/src/translations/fr/common.json b/client/src/translations/fr/common.json index 27fb6551d..d18c96c66 100644 --- a/client/src/translations/fr/common.json +++ b/client/src/translations/fr/common.json @@ -383,6 +383,9 @@ }, "successes": { "save": "" + }, + "validation": { + "useremailmustexist": "" } }, "checklist": {