more error handling

This commit is contained in:
erik-sn committed 2020-11-21 00:30:23 -05:00
1 parent 9403edcb92
commit 2c4c54109f
9 files changed
+101 -23

No files matched your search

+1 -2
View File
@@ -32,8 +32,7 @@
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"recoil": "^0.0.10",
"styled-components": "^5.1.1",
"uuid": "^8.3.0"
"styled-components": "^5.1.1"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
@@ -10,7 +10,7 @@ const useStyles = makeStyles(() => ({
root: {
width: "100%",
bottom: "60px",
position: 'absolute',
position: "absolute",
},
}));
@@ -19,25 +19,32 @@ const ALERT_TIMEOUT = 6000;
export default function AlertBar() {
const classes = useStyles();
const { t } = useTranslation();
const { createMessageGroupResult, setCreateMessageGroupResult } = useAlerts();
const {
createMessageGroupResult,
clearCreateMessageGroupResult,
} = useAlerts();
const [severity, setSeverity] = useState<string | null>(null);
const [open, setOpen] = useState(false);
const [message, setMessage] = useState<string | null>(null);
const handleClose = () => {
setOpen(false);
setCreateMessageGroupResult(null);
clearCreateMessageGroupResult();
};
useEffect(() => {
if (!createMessageGroupResult) return;
const { error, phoneNumber } = createMessageGroupResult
const { error, phoneNumber, duplicate } = createMessageGroupResult;
if (error && phoneNumber) {
const error = `${t("APPS_MESSAGES_INVALID_PHONE_NUMBER")}${phoneNumber}`;
setOpen(true);
setSeverity("error");
setMessage(error);
} else if (error && duplicate) {
setOpen(true);
setSeverity("error");
setMessage(t("APPS_MESSAGES_MESSAGE_GROUP_DUPLICATE"));
} else if (error) {
setOpen(true);
setSeverity("error");
+15 -7
View File
@@ -1,13 +1,21 @@
import { useRecoilState } from 'recoil';
import { useRecoilState } from "recoil";
import { CreateMessageGroupResult } from "../../../common/interfaces/messages";
import { messageState } from './state';
import { messageState } from "./state";
interface IUseAlerts {
createMessageGroupResult: any;
setCreateMessageGroupResult: any;
createMessageGroupResult: CreateMessageGroupResult | null;
clearCreateMessageGroupResult: () => void;
}
export default (): IUseAlerts => {
const [createMessageGroupResult, setCreateMessageGroupResult] = useRecoilState<any>(messageState.createMessageGroupResult)
return { createMessageGroupResult, setCreateMessageGroupResult };
}
const [
createMessageGroupResult,
setCreateMessageGroupResult,
] = useRecoilState<CreateMessageGroupResult | null>(
messageState.createMessageGroupResult
);
const clearCreateMessageGroupResult = () => setCreateMessageGroupResult(null);
return { createMessageGroupResult, clearCreateMessageGroupResult };
};
+7
View File
@@ -17,3 +17,10 @@ export interface MessageGroup {
label?: string;
updatedAt: string;
}
export interface CreateMessageGroupResult {
error?: boolean;
phoneNumber?: string;
duplicate?: boolean;
groupId?: string;
}
+1
View File
@@ -58,6 +58,7 @@
"APPS_MESSAGES_GROUP_CHAT_LABEL": "Group chat label...",
"APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT": "Submit",
"APPS_MESSAGES_INVALID_PHONE_NUMBER": "Invalid phone number: ",
"APPS_MESSAGES_MESSAGE_GROUP_DUPLICATE": "This message group already exists",
"APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED": "Failed to create message group"
}
}
-5
View File
@@ -11820,11 +11820,6 @@ uuid@^3.3.2, uuid@^3.4.0:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
uuid@^8.3.0:
version "8.3.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.1.tgz#2ba2e6ca000da60fce5a196954ab241131e05a31"
integrity sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg==
v8-compile-cache@^2.0.3, v8-compile-cache@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.1.tgz#54bc3cdd43317bca91e35dcaf305b1a7237de745"
+2
View File
@@ -21,6 +21,7 @@
"connection-string-parser": "^1.0.3",
"fivem-esx-js": "^0.1.12",
"fivem-js": "^1.3.1",
"md5": "^2.3.0",
"mysql2": "^2.2.5",
"uuid": "^8.3.1"
},
@@ -28,6 +29,7 @@
"@babel/preset-env": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@types/jest": "^26.0.14",
"@types/md5": "^2.2.1",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^3.8.0",
"@typescript-eslint/parser": "^4.4.0",
+37 -4
View File
@@ -1,7 +1,7 @@
import { v4 as uuidv4 } from 'uuid';
import md5 from 'md5';
import events from '../utils/events';
import { Message, MessageGroup } from '../../phone/src/common/interfaces/messages';
import { Message, MessageGroup, CreateMessageGroupResult } from '../../phone/src/common/interfaces/messages';
import { pool, withTransaction } from "./db";
import { getSource, useIdentifier } from './functions';
@@ -160,6 +160,26 @@ async function getIdentifierFromPhoneNumber(phoneNumber: string): Promise<string
return identifiers[0]['identifier']
}
/**
* This method checks if the input groupId already exists in
* the database. As long as the groupId is derived from the input
* identifiers this means that the user is trying to create a
* duplicate!
* @param groupId - group Id to check that it exists
*/
async function checkIfMessageGroupExists(groupId: string): Promise<boolean> {
const query = `
SELECT COUNT(*) as count
FROM npwd_messages_groups
WHERE group_id = ?;
`;
const [results] = await pool.query(query, [ groupId]);
const result = <any>results;
const count = result[0].count;
return count > 0;
}
/**
* Consolidate raw message groups into a mapping that groups participants
* by the message group. The goal of this is to reduce the rows of message
@@ -223,8 +243,7 @@ async function getFormattedMessageGroups(userIdentifier: string): Promise<Messag
async function createMessageGroupsFromPhoneNumbers(
userIdentifier: string,
phoneNumbers: string[],
groupLabel: string): Promise<any> {
const groupId = uuidv4();
groupLabel: string): Promise<CreateMessageGroupResult> {
// we check that each phoneNumber exists before we create the group
const identifiers: string[] = [];
@@ -237,6 +256,20 @@ async function createMessageGroupsFromPhoneNumbers(
}
}
// make sure we are always in a consistent order. It is very important
// that this not change! Changing this order can result in the ability
// of duplicate message groups being created.
identifiers.sort();
const mergedIdentifiers = identifiers.join('-');
// we don't need this to be secure. Its purpose is to create a unique
// string derived from the identifiers. In this way we can check
// that this groupId isn't used before. If it has then it means
// we are trying to create a duplicate message group!
const groupId = md5(mergedIdentifiers);
if (await checkIfMessageGroupExists(groupId)) {
return { error: true, duplicate: true };
}
const queryPromises = [
// create a row that contains ourselves so reverse accessing from
// other players works as expected
+27 -1
View File
@@ -1245,6 +1245,13 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==
"@types/md5@^2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/md5/-/md5-2.2.1.tgz#0e2d65d8f6cc91aafbc9be5a4c08fee9250406ce"
integrity sha512-bZB0jqBL7JETFqvRKyuDETFceFaVcLm2MBPP5LFEEL/SZuqLnyvzF37tXmMERDncC3oeEj/fOUw88ftJeMpZaw==
dependencies:
"@types/node" "*"
"@types/node@*":
version "14.11.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.11.5.tgz#fecad41c041cae7f2404ad4b2d0742fdb628b305"
@@ -2205,6 +2212,11 @@ char-regex@^1.0.2:
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==
[email protected]:
version "0.0.2"
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
integrity sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=
chokidar@^2.1.8:
version "2.1.8"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.8.tgz#804b3a7b6a99358c3c5c61e71d8728f041cff917"
@@ -2516,6 +2528,11 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"
[email protected]:
version "0.0.2"
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
integrity sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=
crypto-browserify@^3.11.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"
@@ -3868,7 +3885,7 @@ is-binary-path@~2.1.0:
dependencies:
binary-extensions "^2.0.0"
is-buffer@^1.1.5:
is-buffer@^1.1.5, is-buffer@~1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
@@ -4816,6 +4833,15 @@ md5.js@^1.3.4:
inherits "^2.0.1"
safe-buffer "^5.1.2"
md5@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
integrity sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==
dependencies:
charenc "0.0.2"
crypt "0.0.2"
is-buffer "~1.1.6"
memory-fs@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"