The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(882): fixes negative number submission for employees input (#1130)
Srikar Samudrala4717f4cb9089
diff --git a/front/src/modules/companies/constants/companyViewFields.tsx b/front/src/modules/companies/constants/companyViewFields.tsxindex 6d842ab94d2..57b4761604a 100644--- a/front/src/modules/companies/constants/companyViewFields.tsx+++ b/front/src/modules/companies/constants/companyViewFields.tsx@@ -81,6 +81,7 @@ export const companyViewFields: ViewFieldDefinition<ViewFieldMetadata>[] = [metadata: {type: 'number',fieldName: 'employees',+ isPositive: true,},isVisible: true,} satisfies ViewFieldDefinition<ViewFieldNumberMetadata>,diff --git a/front/src/modules/ui/editable-field/types/ViewField.ts b/front/src/modules/ui/editable-field/types/ViewField.tsindex 695ecfdbbc3..7989dd9dbe5 100644--- a/front/src/modules/ui/editable-field/types/ViewField.ts+++ b/front/src/modules/ui/editable-field/types/ViewField.ts@@ -39,6 +39,7 @@ export type ViewFieldDateMetadata = {export type ViewFieldNumberMetadata = {type: 'number';fieldName: string;+ isPositive: boolean;};export type ViewFieldRelationMetadata = {diff --git a/front/src/modules/ui/table/editable-cell/type/components/GenericEditableNumberCellEditMode.tsx b/front/src/modules/ui/table/editable-cell/type/components/GenericEditableNumberCellEditMode.tsxindex 39b77ba2f6d..89db636a78d 100644--- a/front/src/modules/ui/table/editable-cell/type/components/GenericEditableNumberCellEditMode.tsx+++ b/front/src/modules/ui/table/editable-cell/type/components/GenericEditableNumberCellEditMode.tsx@@ -7,6 +7,10 @@ import {import { useCurrentRowEntityId } from '@/ui/table/hooks/useCurrentEntityId';import { useUpdateEntityField } from '@/ui/table/hooks/useUpdateEntityField';import { tableEntityFieldFamilySelector } from '@/ui/table/states/tableEntityFieldFamilySelector';+import {+ canBeCastAsPositiveIntegerOrNull,+ castAsPositiveIntegerOrNull,+} from '~/utils/cast-as-positive-integer-or-null';import { TextCellEdit } from './TextCellEdit';@@ -31,12 +35,27 @@ export function GenericEditableNumberCellEditMode({ viewField }: OwnProps) {if (newText === fieldValue) return;try {- const numberValue = parseInt(newText);+ let numberValue = parseInt(newText);if (isNaN(numberValue)) {throw new Error('Not a number');}+ if (viewField.metadata.isPositive) {+ if (!canBeCastAsPositiveIntegerOrNull(newText)) {+ return;+ }++ const valueCastedAsPositiveNumberOrNull =+ castAsPositiveIntegerOrNull(newText);++ if (valueCastedAsPositiveNumberOrNull === null) {+ throw Error('Not a number');+ }++ numberValue = valueCastedAsPositiveNumberOrNull;+ }+// TODO: find a way to store this better in DBif (numberValue > 2000000000) {throw new Error('Number too big');diff --git a/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts b/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.tsnew file mode 100644index 00000000000..1c3e9d10b3d--- /dev/null+++ b/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts@@ -0,0 +1,116 @@+import {+ canBeCastAsPositiveIntegerOrNull,+ castAsPositiveIntegerOrNull,+} from '~/utils/cast-as-positive-integer-or-null';++describe('canBeCastAsPositiveIntegerOrNull', () => {+ it(`should return true if null`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(null)).toBeTruthy();+ });++ it(`should return true if positive number`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(9)).toBeTruthy();+ });++ it(`should return false if negative number`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(-9)).toBeFalsy();+ });++ it(`should return false if zero`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(0)).toBeFalsy();+ });++ it(`should return false if string 0`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('0')).toBeFalsy();+ });++ it(`should return false if negative float`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(-1.22)).toBeFalsy();+ });++ it(`should return false if positive float`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(1.22)).toBeFalsy();+ });++ it(`should return false if positive float string`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('0.9')).toBeFalsy();+ });++ it(`should return false if negative float string`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('-0.9')).toBeFalsy();+ });++ it(`should return false if less than 1`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(0.22)).toBeFalsy();+ });++ it(`should return true if empty string`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('')).toBeTruthy();+ });++ it(`should return true if integer string`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('9')).toBeTruthy();+ });++ it(`should return false if undefined`, () => {+ expect(canBeCastAsPositiveIntegerOrNull(undefined)).toBeFalsy();+ });++ it(`should return false if non numeric string`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('9a')).toBeFalsy();+ });++ it(`should return false if non numeric string #2`, () => {+ expect(canBeCastAsPositiveIntegerOrNull('a9a')).toBeFalsy();+ });+});++describe('castAsPositiveIntegerOrNull', () => {+ it(`should cast null to null`, () => {+ expect(castAsPositiveIntegerOrNull(null)).toBe(null);+ });++ it(`should cast empty string to null`, () => {+ expect(castAsPositiveIntegerOrNull('')).toBe(null);+ });++ it(`should cast an integer to positive integer`, () => {+ expect(castAsPositiveIntegerOrNull(9)).toBe(9);+ });++ it(`should cast an integer string to positive integer`, () => {+ expect(castAsPositiveIntegerOrNull('9')).toBe(9);+ });++ it(`should throw if trying to cast a 0 to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull(0)).toThrow(Error);+ });++ it(`should throw if trying to cast a string 0 to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull('0')).toThrow(Error);+ });++ it(`should throw if trying to cast a positive float string to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull('9.9')).toThrow(Error);+ });++ it(`should throw if trying to cast a negative float string to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull('-9.9')).toThrow(Error);+ });++ it(`should throw if trying to cast a positive float to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull(9.9)).toThrow(Error);+ });++ it(`should throw if trying to cast a negative float to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull(-9.9)).toThrow(Error);+ });++ it(`should throw if trying to cast a non numeric string to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull('9.9a')).toThrow(Error);+ });++ it(`should throw if trying to cast an undefined to positive integer`, () => {+ expect(() => castAsPositiveIntegerOrNull(undefined)).toThrow(Error);+ });+});diff --git a/front/src/utils/cast-as-positive-integer-or-null.ts b/front/src/utils/cast-as-positive-integer-or-null.tsnew file mode 100644index 00000000000..56ad707fe56--- /dev/null+++ b/front/src/utils/cast-as-positive-integer-or-null.ts@@ -0,0 +1,64 @@+export function canBeCastAsPositiveIntegerOrNull(+ probablePositiveNumberOrNull: string | undefined | number | null,+): probablePositiveNumberOrNull is number | null {+ if (probablePositiveNumberOrNull === undefined) {+ return false;+ }++ if (typeof probablePositiveNumberOrNull === 'number') {+ return (+ Number.isInteger(probablePositiveNumberOrNull) &&+ Math.sign(probablePositiveNumberOrNull) === 1+ );+ }++ if (probablePositiveNumberOrNull === null) {+ return true;+ }++ if (probablePositiveNumberOrNull === '') {+ return true;+ }++ if (typeof probablePositiveNumberOrNull === 'string') {+ const stringAsNumber = +probablePositiveNumberOrNull;++ if (isNaN(stringAsNumber)) {+ return false;+ }++ if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) === 1) {+ return true;+ }+ }++ return false;+}++export function castAsPositiveIntegerOrNull(+ probablePositiveNumberOrNull: string | undefined | number | null,+): number | null {+ if (+ canBeCastAsPositiveIntegerOrNull(probablePositiveNumberOrNull) === false+ ) {+ throw new Error('Cannot cast to positive number or null');+ }++ if (probablePositiveNumberOrNull === null) {+ return null;+ }++ if (probablePositiveNumberOrNull === '') {+ return null;+ }++ if (typeof probablePositiveNumberOrNull === 'number') {+ return probablePositiveNumberOrNull;+ }++ if (typeof probablePositiveNumberOrNull === 'string') {+ return +probablePositiveNumberOrNull;+ }++ return null;+}