diff --git a/API.attributename.md b/API.attributename.md index c5a1dbe..f3c9bab 100644 --- a/API.attributename.md +++ b/API.attributename.md @@ -64,7 +64,7 @@ Lists the attribute names for the user's selected company, including values. **Returns** -`Promise}>>` +`Promise}>>` ## `attributename.order` diff --git a/API.attributevalue.md b/API.attributevalue.md index 373a6a6..3c7eafc 100644 --- a/API.attributevalue.md +++ b/API.attributevalue.md @@ -106,7 +106,7 @@ Lists the values for an attribute. **Returns** -`Promise>` +`Promise>` ## `attributevalue.order` diff --git a/API.md b/API.md index f211ab9..fd3d40d 100644 --- a/API.md +++ b/API.md @@ -251,7 +251,7 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met - [propertyname](API.propertyname.md) - `create(googleAuthId: number, input: {name: string, sort?: number, exclusive?: boolean}, tx?: object): Promise<{id: number, name: string, sort: number, exclusive: boolean}>` - `delete(googleAuthId: number, id: number, tx?: object): Promise` - - `list(googleAuthId: number, tx?: object): Promise>` + - `list(googleAuthId: number, tx?: object): Promise>` - `order(googleAuthId: number, input: {propertynameIds: Array}, tx?: object): Promise<{propertynameIds: Array}>` - `update(googleAuthId: number, id: number, input: {name?: string, sort?: number, exclusive?: boolean}, tx?: object): Promise<{id: number, name: string, sort: number, exclusive: boolean}>` - [articleproperty](API.articleproperty.md) @@ -261,7 +261,7 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met - [attributename](API.attributename.md) - `create(googleAuthId: number, input: {name: string, sort?: number, exclusive?: boolean}, tx?: object): Promise<{id: number, name: string, sort: number, exclusive: boolean, values: Array}>` - `delete(googleAuthId: number, id: number, tx?: object): Promise` - - `list(googleAuthId: number, tx?: object): Promise}>>` + - `list(googleAuthId: number, tx?: object): Promise}>>` - `order(googleAuthId: number, input: {attributenameIds: Array}, tx?: object): Promise<{attributenameIds: Array}>` - `update(googleAuthId: number, id: number, input: {name?: string, sort?: number, exclusive?: boolean}, tx?: object): Promise<{id: number, name: string, sort: number, exclusive: boolean, values: Array}>` - [attributevalue](API.attributevalue.md) @@ -269,7 +269,7 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met - `create(googleAuthId: number, attributeId: number, input: {value: string, sort?: number}, tx?: object): Promise<{id: number, attributeId: number, value: string, sort: number}>` - `delete(googleAuthId: number, id: number, tx?: object): Promise` - `deleteImage(googleAuthId: number, attributeValueId: number, tx?: object): Promise<{id: number, thumbW: null, thumbH: null, thumbHash: null, pics: Array}>` - - `listByAttributeId(googleAuthId: number, attributeId: number, tx?: object): Promise>` + - `listByAttributeId(googleAuthId: number, attributeId: number, tx?: object): Promise>` - `order(googleAuthId: number, attributeId: number, input: {attributevalueIds: Array}, tx?: object): Promise<{attributevalueIds: Array}>` - `update(googleAuthId: number, id: number, input: {value?: string, sort?: number}, tx?: object): Promise<{id: number, attributeId: number, value: string, sort: number}>` - [articleattributevalue](API.articleattributevalue.md) diff --git a/API.propertyname.md b/API.propertyname.md index 225bb6a..c652fff 100644 --- a/API.propertyname.md +++ b/API.propertyname.md @@ -64,7 +64,7 @@ Lists the property names for the user's selected company. **Returns** -`Promise>` +`Promise>` ## `propertyname.order` diff --git a/src/providers/mssql/queries/attributename.js b/src/providers/mssql/queries/attributename.js index 3d5bc44..5c057dc 100644 --- a/src/providers/mssql/queries/attributename.js +++ b/src/providers/mssql/queries/attributename.js @@ -4,11 +4,25 @@ SELECT an.name, an.sort, an.exclusive, + ( + SELECT COUNT(*) + FROM article a + WHERE a.parentattributename_id = an.id + AND a.is_parent = 1 + AND a.company_id = an.company_id + ) AS articleCount, ( SELECT av.id, av.value, - av.sort + av.sort, + ( + SELECT COUNT(DISTINCT aav.article_id) + FROM articleattributevalue aav + INNER JOIN article a ON a.id = aav.article_id + WHERE aav.attributevalue_id = av.id + AND a.company_id = an.company_id + ) AS articleCount FROM attributevalue av WHERE av.attribute_id = an.id ORDER BY av.sort ASC, av.id ASC @@ -33,11 +47,25 @@ SELECT an.name, an.sort, an.exclusive, + ( + SELECT COUNT(*) + FROM article a + WHERE a.parentattributename_id = an.id + AND a.is_parent = 1 + AND a.company_id = an.company_id + ) AS articleCount, ( SELECT av.id, av.value, - av.sort + av.sort, + ( + SELECT COUNT(DISTINCT aav.article_id) + FROM articleattributevalue aav + INNER JOIN article a ON a.id = aav.article_id + WHERE aav.attributevalue_id = av.id + AND a.company_id = an.company_id + ) AS articleCount FROM attributevalue av WHERE av.attribute_id = an.id ORDER BY av.sort ASC, av.id ASC diff --git a/src/providers/mssql/queries/attributevalue.js b/src/providers/mssql/queries/attributevalue.js index 50089a8..a12604a 100644 --- a/src/providers/mssql/queries/attributevalue.js +++ b/src/providers/mssql/queries/attributevalue.js @@ -3,7 +3,14 @@ SELECT av.id, av.attribute_id AS attributeId, av.value, - av.sort + av.sort, + ( + SELECT COUNT(DISTINCT aav.article_id) + FROM articleattributevalue aav + INNER JOIN article a ON a.id = aav.article_id + WHERE aav.attributevalue_id = av.id + AND a.company_id = an.company_id + ) AS articleCount FROM attributevalue av INNER JOIN attributename an ON an.id = av.attribute_id WHERE av.attribute_id = @attributeId AND an.company_id = @companyId diff --git a/src/providers/mssql/queries/propertyname.js b/src/providers/mssql/queries/propertyname.js index 8ccd003..2ccc7c4 100644 --- a/src/providers/mssql/queries/propertyname.js +++ b/src/providers/mssql/queries/propertyname.js @@ -1,12 +1,19 @@ export const LIST_BY_COMPANY = ` SELECT - id, - name, - sort, - exclusive -FROM propertyname -WHERE company_id = @companyId -ORDER BY sort ASC, id ASC + pn.id, + pn.name, + pn.sort, + pn.exclusive, + ( + SELECT COUNT(DISTINCT ap.article_id) + FROM articleproperty ap + INNER JOIN article a ON a.id = ap.article_id + WHERE ap.property_id = pn.id + AND a.company_id = pn.company_id + ) AS articleCount +FROM propertyname pn +WHERE pn.company_id = @companyId +ORDER BY pn.sort ASC, pn.id ASC `; export const LIST_IDS_BY_COMPANY = ` diff --git a/src/providers/sqlite3/queries/attributename.js b/src/providers/sqlite3/queries/attributename.js index b077fcf..1df930c 100644 --- a/src/providers/sqlite3/queries/attributename.js +++ b/src/providers/sqlite3/queries/attributename.js @@ -18,14 +18,32 @@ SELECT an.name, an.sort, an.exclusive, + ( + SELECT COUNT(*) + FROM article a + WHERE a.parentattributename_id = an.id + AND a.is_parent = 1 + AND a.company_id = an.company_id + ) AS articleCount, ( SELECT COALESCE(json_group_array(json_object( 'id', id, 'value', value, - 'sort', sort + 'sort', sort, + 'articleCount', articleCount )), '[]') FROM ( - SELECT av.id, av.value, av.sort + SELECT + av.id, + av.value, + av.sort, + ( + SELECT COUNT(DISTINCT aav.article_id) + FROM articleattributevalue aav + INNER JOIN article a ON a.id = aav.article_id + WHERE aav.attributevalue_id = av.id + AND a.company_id = an.company_id + ) AS articleCount FROM attributevalue av WHERE av.attribute_id = an.id ORDER BY av.sort ASC, av.id ASC @@ -43,14 +61,32 @@ SELECT an.name, an.sort, an.exclusive, + ( + SELECT COUNT(*) + FROM article a + WHERE a.parentattributename_id = an.id + AND a.is_parent = 1 + AND a.company_id = an.company_id + ) AS articleCount, ( SELECT COALESCE(json_group_array(json_object( 'id', id, 'value', value, - 'sort', sort + 'sort', sort, + 'articleCount', articleCount )), '[]') FROM ( - SELECT av.id, av.value, av.sort + SELECT + av.id, + av.value, + av.sort, + ( + SELECT COUNT(DISTINCT aav.article_id) + FROM articleattributevalue aav + INNER JOIN article a ON a.id = aav.article_id + WHERE aav.attributevalue_id = av.id + AND a.company_id = an.company_id + ) AS articleCount FROM attributevalue av WHERE av.attribute_id = an.id ORDER BY av.sort ASC, av.id ASC diff --git a/src/store/attributename/list.js b/src/store/attributename/list.js index 32265f4..f4ac482 100644 --- a/src/store/attributename/list.js +++ b/src/store/attributename/list.js @@ -18,7 +18,7 @@ export default function list(provider) { * * @param {number} googleAuthId - The id of the Google auth user. * @param {object} [tx] - Optional transaction. - * @returns {Promise}>>} + * @returns {Promise}>>} */ return async (googleAuthId, tx) => { const { companyId } = await access(googleAuthId, tx); @@ -28,7 +28,13 @@ export default function list(provider) { name: row.name, sort: row.sort, exclusive: Boolean(row.exclusive), - values: parseValues(row.valuesJson), + articleCount: Number(row.articleCount ?? 0), + values: parseValues(row.valuesJson).map((v) => ({ + id: v.id, + value: v.value, + sort: v.sort, + articleCount: Number(v.articleCount ?? 0), + })), })); }; } diff --git a/src/store/attributevalue/listByAttributeId.js b/src/store/attributevalue/listByAttributeId.js index 560c4d3..4b8144b 100644 --- a/src/store/attributevalue/listByAttributeId.js +++ b/src/store/attributevalue/listByAttributeId.js @@ -13,7 +13,7 @@ export default function listByAttributeId(provider) { * @param {number} googleAuthId - The id of the Google auth user. * @param {number} attributeId - The attribute id. * @param {object} [tx] - Optional transaction. - * @returns {Promise>} + * @returns {Promise>} */ return async (googleAuthId, attributeId, tx) => { if (!attributeId || !Number.isInteger(Number(attributeId))) { @@ -29,6 +29,7 @@ export default function listByAttributeId(provider) { attributeId: row.attributeId, value: row.value, sort: row.sort, + articleCount: Number(row.articleCount ?? 0), })); }; } diff --git a/src/store/propertyname/list.js b/src/store/propertyname/list.js index 1496719..cff40a2 100644 --- a/src/store/propertyname/list.js +++ b/src/store/propertyname/list.js @@ -12,7 +12,7 @@ export default function list(provider) { * * @param {number} googleAuthId - The id of the Google auth user. * @param {object} [tx] - Optional transaction. - * @returns {Promise>} + * @returns {Promise>} */ return async (googleAuthId, tx) => { const { companyId } = await access(googleAuthId, tx); @@ -22,6 +22,7 @@ export default function list(provider) { name: row.name, sort: row.sort, exclusive: Boolean(row.exclusive), + articleCount: Number(row.articleCount ?? 0), })); }; }