Skip to content

Commit

Permalink
Merge branch 'master' into hidden-category-grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazib authored Nov 20, 2023
2 parents a09ab48 + 9c6d9ec commit 5613c6f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/desktop-client/src/components/budget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type BudgetProps = {
moveCategory: BoundActions['moveCategory'];
moveCategoryGroup: BoundActions['moveCategoryGroup'];
loadPrefs: BoundActions['loadPrefs'];
addNotification: BoundActions['addNotification'];
};

function Budget(props: BudgetProps) {
Expand Down Expand Up @@ -241,7 +242,28 @@ function Budget(props: BudgetProps) {
setIsAddingGroup(false);
};

const categoryNameAlreadyExistsNotification = name => {
props.addNotification({
type: 'error',
message: `Category ‘${name}’ already exists in group (May be Hidden)`,
});
};

const onSaveCategory = async category => {
let exists =
(await props.getCategories()).grouped
.filter(g => g.id === category.cat_group)[0]
.categories.filter(
c => c.name.toUpperCase() === category.name.toUpperCase(),
)
.filter(c => (category.id === 'new' ? true : c.id !== category.id))
.length > 0;

if (exists) {
categoryNameAlreadyExistsNotification(category.name);
return;
}

if (category.id === 'new') {
let id = await props.createCategory(
category.name,
Expand Down Expand Up @@ -350,6 +372,21 @@ function Budget(props: BudgetProps) {
};

const onReorderCategory = async sortInfo => {
let cats = await props.getCategories();
let moveCandidate = cats.list.filter(c => c.id === sortInfo.id)[0];
let exists =
cats.grouped
.filter(g => g.id === sortInfo.groupId)[0]
.categories.filter(
c => c.name.toUpperCase() === moveCandidate.name.toUpperCase(),
)
.filter(c => c.id !== moveCandidate.id).length > 0;

if (exists) {
categoryNameAlreadyExistsNotification(moveCandidate.name);
return;
}

props.moveCategory(sortInfo.id, sortInfo.groupId, sortInfo.targetId);
setCategoryGroups(state =>
moveCategory(state, sortInfo.id, sortInfo.groupId, sortInfo.targetId),
Expand Down
11 changes: 11 additions & 0 deletions packages/loot-core/src/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,17 @@ export async function insertCategory(

let id_;
await batchMessages(async () => {
// Dont allow duplicated names in groups
const existingCatInGroup = await first(
`SELECT id FROM categories WHERE cat_group = ? and UPPER(name) = ? LIMIT 1`,
[category.cat_group, category.name.toUpperCase()],
);
if (existingCatInGroup) {
throw new Error(
`Category ‘${category.name}’ already exists in group ‘${category.cat_group}’`,
);
}

if (atEnd) {
const lastCat = await first(`
SELECT sort_order FROM categories WHERE tombstone = 0 ORDER BY sort_order DESC, id DESC LIMIT 1
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1833.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [Shazib]
---

Not allowed duplicated Category names in Category Groups

0 comments on commit 5613c6f

Please sign in to comment.