@littlebox/strapi-suite 1.0.52 → 1.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -542,12 +542,122 @@ const bootstrap = async ({ strapi: strapi2 }) => {
542
542
  };
543
543
  const destroy = ({ strapi: strapi2 }) => {
544
544
  };
545
+ const TABS_PLUGIN_KEY = "littlebox-strapi-suite";
546
+ const SYSTEM_KEYS = [
547
+ "id",
548
+ "documentId",
549
+ "locale",
550
+ "createdAt",
551
+ "updatedAt",
552
+ "publishedAt",
553
+ "createdBy",
554
+ "updatedBy",
555
+ "localizations"
556
+ ];
557
+ function toTabKey(name) {
558
+ return (name ?? "").normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/\s+/g, "_").replace(/[^a-z0-9_]/g, "").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
559
+ }
560
+ function readTabs(pluginOptions2) {
561
+ const tabs = pluginOptions2?.[TABS_PLUGIN_KEY]?.tabs;
562
+ if (!Array.isArray(tabs)) return [];
563
+ return tabs.filter((tab) => tab && typeof tab.id === "string" && typeof tab.name === "string");
564
+ }
565
+ function readAttributeTabId(attribute) {
566
+ const id = attribute?.pluginOptions?.[TABS_PLUGIN_KEY]?.tab;
567
+ return typeof id === "string" && id !== "" ? id : null;
568
+ }
569
+ function buildTabGroups(tabs, attributes2, onConflict) {
570
+ const candidates = tabs.map((tab) => ({
571
+ tab,
572
+ key: toTabKey(tab.name),
573
+ fields: Object.entries(attributes2).filter(([, attribute]) => readAttributeTabId(attribute) === tab.id).map(([name]) => name)
574
+ })).filter((group) => group.fields.length > 0);
575
+ const reservedKeys = /* @__PURE__ */ new Set([...SYSTEM_KEYS, ...Object.keys(attributes2)]);
576
+ const usedKeys = /* @__PURE__ */ new Set();
577
+ return candidates.filter((group) => {
578
+ if (group.key === "" || reservedKeys.has(group.key) || usedKeys.has(group.key)) {
579
+ onConflict?.(group.tab, group.key);
580
+ return false;
581
+ }
582
+ usedKeys.add(group.key);
583
+ return true;
584
+ });
585
+ }
586
+ function groupEntryByTabs(entry, groups) {
587
+ if (!entry || typeof entry !== "object" || Array.isArray(entry) || groups.length === 0) {
588
+ return entry;
589
+ }
590
+ const fieldToKey = /* @__PURE__ */ new Map();
591
+ groups.forEach((group) => group.fields.forEach((field) => fieldToKey.set(field, group.key)));
592
+ const result = {};
593
+ const grouped = {};
594
+ Object.entries(entry).forEach(([name, value]) => {
595
+ const key = fieldToKey.get(name);
596
+ if (key === void 0) {
597
+ result[name] = value;
598
+ } else {
599
+ (grouped[key] ??= {})[name] = value;
600
+ }
601
+ });
602
+ groups.forEach((group) => {
603
+ if (grouped[group.key]) result[group.key] = grouped[group.key];
604
+ });
605
+ return result;
606
+ }
607
+ function groupDataByTabs(data, groups) {
608
+ if (groups.length === 0) return data;
609
+ return Array.isArray(data) ? data.map((entry) => groupEntryByTabs(entry, groups)) : groupEntryByTabs(data, groups);
610
+ }
611
+ function parseHandlerUid(handler) {
612
+ if (typeof handler !== "string") return null;
613
+ const match = handler.match(/^(.+)\.(find|findOne)$/);
614
+ return match ? match[1] : null;
615
+ }
616
+ function createContentTypeTabsGrouper(strapi2) {
617
+ const warned = /* @__PURE__ */ new Set();
618
+ return function groupByContentTypeTabs(uid, data) {
619
+ try {
620
+ const contentType = strapi2.contentType(uid);
621
+ if (!contentType) return data;
622
+ const tabs = readTabs(contentType.pluginOptions);
623
+ if (tabs.length === 0) return data;
624
+ const groups = buildTabGroups(tabs, contentType.attributes ?? {}, (tab, key) => {
625
+ const id = `${uid}:${tab.id}`;
626
+ if (warned.has(id)) return;
627
+ warned.add(id);
628
+ strapi2.log.warn(
629
+ `[littlebox-strapi-suite] Tab "${tab.name}" of ${uid} was not grouped: the key "${key}" is empty or conflicts with a field or another tab.`
630
+ );
631
+ });
632
+ return groupDataByTabs(data, groups);
633
+ } catch (error) {
634
+ strapi2.log.error(`[littlebox-strapi-suite] Could not group ${uid} data by tabs: ${error}`);
635
+ return data;
636
+ }
637
+ };
638
+ }
639
+ function createTabsMiddleware({ strapi: strapi2 }) {
640
+ const groupByContentTypeTabs = createContentTypeTabsGrouper(strapi2);
641
+ return async (ctx, next) => {
642
+ await next();
643
+ if (ctx.method !== "GET" || ctx.status !== 200) return;
644
+ const prefix = strapi2.config.get("api.rest.prefix", "/api");
645
+ if (!ctx.path?.startsWith(prefix)) return;
646
+ const body = ctx.body;
647
+ if (!body || typeof body !== "object" || body.data == null) return;
648
+ const uid = parseHandlerUid(ctx.state?.route?.handler);
649
+ if (!uid) return;
650
+ const data = groupByContentTypeTabs(uid, body.data);
651
+ if (data !== body.data) ctx.body = { ...body, data };
652
+ };
653
+ }
545
654
  const register = ({ strapi: strapi2 }) => {
546
655
  strapi2.customFields.register({
547
656
  name: "ltbslug",
548
657
  plugin: PLUGIN_ID,
549
658
  type: "uid"
550
659
  });
660
+ strapi2.server.use(createTabsMiddleware({ strapi: strapi2 }));
551
661
  };
552
662
  const kind$7 = "collectionType";
553
663
  const collectionName$7 = "ltb_st_app_setting";
@@ -1892,210 +2002,108 @@ async function findDocumentIdsAndFetchSlugs(params) {
1892
2002
  await Promise.all(promises);
1893
2003
  return params.document;
1894
2004
  }
1895
- const SlugModuleService = ({ strapi: strapi2 }) => ({
1896
- async adminGetAll() {
1897
- const ctx = strapi2.requestContext.get();
1898
- if (!ctx.query.locale) return [];
1899
- const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
1900
- const documents = await strapi2.db.query(config2.uuid.modules.slug).findMany({
1901
- where: {
1902
- locale: ctx.query.locale,
1903
- state: "published"
1904
- },
1905
- orderBy: {
1906
- id: "desc"
1907
- }
1908
- });
1909
- const publishedResults = [];
1910
- for (const document of documents) {
1911
- const content = await strapi2.documents(document.contentModel).findOne({
1912
- status: "published",
1913
- documentId: document.contentId,
1914
- locale: ctx.query.locale
1915
- });
1916
- if (content) {
1917
- document.contentTitle = content.title || `${content.documentId} | ${document.contentModel}`;
1918
- publishedResults.push(document);
1919
- }
1920
- }
1921
- return publishedResults || [];
1922
- },
1923
- async adminBulkDelete() {
1924
- const ctx = strapi2.requestContext.get();
1925
- if (!ctx.query.locale) return;
1926
- const body = ctx.request.body;
1927
- if (!body.contentIds) return;
1928
- const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
1929
- await strapi2.db.query(config2.uuid.modules.slug).deleteMany({
1930
- where: {
1931
- contentId: body.contentIds,
1932
- locale: ctx.query.locale
1933
- }
1934
- });
1935
- ctx.status = 204;
1936
- return;
1937
- },
1938
- async getPage() {
1939
- const ctx = strapi2.requestContext.get();
1940
- const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
1941
- const slug = ctx.query.slug.replace(/^\/|\/$/g, "");
1942
- const locales = await strapi2.plugin("i18n").service("locales").find();
1943
- const mappedLocales = locales.map((locale) => locale.code.toLowerCase());
1944
- const defaultLocale = await strapi2.plugin("i18n").service("locales").getDefaultLocale();
1945
- const settings = await strapi2.db.query(config2.uuid.app.setting).findMany({
1946
- where: {
1947
- module: "slug",
1948
- property: [
1949
- "showDefaultLanguage",
1950
- "homepageContentId",
1951
- "homepageContentModel",
1952
- "homepageSlugStrategy"
1953
- ]
1954
- }
1955
- });
1956
- const showDefaultLanguage = settings.find((setting) => setting.property === "showDefaultLanguage").value === "true";
1957
- const homepageContentId = settings.find(
1958
- (setting) => setting.property === "homepageContentId"
1959
- ).value;
1960
- const homepageSlugStrategy = settings.find(
1961
- (setting) => setting.property === "homepageSlugStrategy"
1962
- ).value;
1963
- const homepageContentModel = settings.find(
1964
- (setting) => setting.property === "homepageContentModel"
1965
- ).value;
1966
- let page;
1967
- if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY && mappedLocales.includes(slug)) {
1968
- page = await strapi2.db.query(config2.uuid.modules.slug).findOne({
2005
+ const SlugModuleService = ({ strapi: strapi2 }) => {
2006
+ const groupByContentTypeTabs = createContentTypeTabsGrouper(strapi2);
2007
+ return {
2008
+ async adminGetAll() {
2009
+ const ctx = strapi2.requestContext.get();
2010
+ if (!ctx.query.locale) return [];
2011
+ const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2012
+ const documents = await strapi2.db.query(config2.uuid.modules.slug).findMany({
1969
2013
  where: {
1970
- state: "published",
1971
- contentId: homepageContentId,
1972
- contentModel: homepageContentModel,
1973
- locale: slug
2014
+ locale: ctx.query.locale,
2015
+ state: "published"
2016
+ },
2017
+ orderBy: {
2018
+ id: "desc"
1974
2019
  }
1975
2020
  });
1976
- page.fullSlug = slug;
1977
- } else {
1978
- const pages = await strapi2.db.query(config2.uuid.modules.slug).findMany({
2021
+ const publishedResults = [];
2022
+ for (const document of documents) {
2023
+ const content = await strapi2.documents(document.contentModel).findOne({
2024
+ status: "published",
2025
+ documentId: document.contentId,
2026
+ locale: ctx.query.locale
2027
+ });
2028
+ if (content) {
2029
+ document.contentTitle = content.title || `${content.documentId} | ${document.contentModel}`;
2030
+ publishedResults.push(document);
2031
+ }
2032
+ }
2033
+ return publishedResults || [];
2034
+ },
2035
+ async adminBulkDelete() {
2036
+ const ctx = strapi2.requestContext.get();
2037
+ if (!ctx.query.locale) return;
2038
+ const body = ctx.request.body;
2039
+ if (!body.contentIds) return;
2040
+ const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2041
+ await strapi2.db.query(config2.uuid.modules.slug).deleteMany({
1979
2042
  where: {
1980
- state: "published",
1981
- slug: slug.split("/")
2043
+ contentId: body.contentIds,
2044
+ locale: ctx.query.locale
1982
2045
  }
1983
2046
  });
1984
- pages.forEach((page2) => {
1985
- page2.fullSlug = buildFullSlug({ pages, page: page2, defaultLocale, showDefaultLanguage });
1986
- });
1987
- page = pages.find((page2) => page2.fullSlug === slug);
1988
- }
1989
- if (!page) {
1990
- ctx.status = 404;
2047
+ ctx.status = 204;
1991
2048
  return;
1992
- }
1993
- const properties = ctx.query.properties;
1994
- if (properties && properties.includes("attributes")) {
1995
- const attributes2 = await strapi2.db.query(config2.uuid.modules.attribute).findOne({
2049
+ },
2050
+ async getPage() {
2051
+ const ctx = strapi2.requestContext.get();
2052
+ const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2053
+ const slug = ctx.query.slug.replace(/^\/|\/$/g, "");
2054
+ const locales = await strapi2.plugin("i18n").service("locales").find();
2055
+ const mappedLocales = locales.map((locale) => locale.code.toLowerCase());
2056
+ const defaultLocale = await strapi2.plugin("i18n").service("locales").getDefaultLocale();
2057
+ const settings = await strapi2.db.query(config2.uuid.app.setting).findMany({
1996
2058
  where: {
1997
- locale: page.locale,
1998
- contentId: page.contentId,
1999
- contentModel: page.contentModel,
2000
- state: "published"
2059
+ module: "slug",
2060
+ property: [
2061
+ "showDefaultLanguage",
2062
+ "homepageContentId",
2063
+ "homepageContentModel",
2064
+ "homepageSlugStrategy"
2065
+ ]
2001
2066
  }
2002
2067
  });
2003
- const template = attributes2 && attributes2.templateId ? await strapi2.db.query(config2.uuid.modules.template).findOne({
2004
- where: {
2005
- documentId: attributes2.templateId
2006
- }
2007
- }) : null;
2008
- page.attributes = {
2009
- template: template?.uid || "default",
2010
- priority: attributes2?.priority || "0.5",
2011
- frequency: attributes2?.frequency || "weekly",
2012
- ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2013
- };
2014
- }
2015
- const query = populateQueryFromContentType(strapi2, page.contentModel);
2016
- const document = await strapi2.documents(page.contentModel).findOne({
2017
- locale: page.locale,
2018
- populate: query,
2019
- status: "published",
2020
- documentId: page.contentId
2021
- });
2022
- await findDocumentIdsAndFetchSlugs({
2023
- document,
2024
- defaultLocale,
2025
- showDefaultLanguage
2026
- });
2027
- for (const localization of document.localizations) {
2028
- if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY && localization.documentId === homepageContentId) {
2029
- localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
2068
+ const showDefaultLanguage = settings.find((setting) => setting.property === "showDefaultLanguage").value === "true";
2069
+ const homepageContentId = settings.find(
2070
+ (setting) => setting.property === "homepageContentId"
2071
+ ).value;
2072
+ const homepageSlugStrategy = settings.find(
2073
+ (setting) => setting.property === "homepageSlugStrategy"
2074
+ ).value;
2075
+ const homepageContentModel = settings.find(
2076
+ (setting) => setting.property === "homepageContentModel"
2077
+ ).value;
2078
+ let page;
2079
+ if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY && mappedLocales.includes(slug)) {
2080
+ page = await strapi2.db.query(config2.uuid.modules.slug).findOne({
2081
+ where: {
2082
+ state: "published",
2083
+ contentId: homepageContentId,
2084
+ contentModel: homepageContentModel,
2085
+ locale: slug
2086
+ }
2087
+ });
2088
+ page.fullSlug = slug;
2030
2089
  } else {
2031
- localization.slug = await getSlugByDocumentId({
2032
- contentId: localization.documentId,
2033
- locale: localization.locale,
2034
- defaultLocale,
2035
- showDefaultLanguage,
2036
- strapi: strapi2
2090
+ const pages = await strapi2.db.query(config2.uuid.modules.slug).findMany({
2091
+ where: {
2092
+ state: "published",
2093
+ slug: slug.split("/")
2094
+ }
2037
2095
  });
2096
+ pages.forEach((page2) => {
2097
+ page2.fullSlug = buildFullSlug({ pages, page: page2, defaultLocale, showDefaultLanguage });
2098
+ });
2099
+ page = pages.find((page2) => page2.fullSlug === slug);
2038
2100
  }
2039
- }
2040
- delete document.createdBy;
2041
- delete document.updatedBy;
2042
- document.slug = page.fullSlug;
2043
- document.breadcrumbs = await buildBreadcrumbs({
2044
- strapi: strapi2,
2045
- page,
2046
- defaultLocale,
2047
- showDefaultLanguage,
2048
- homepageSlugStrategy,
2049
- homepageContentId,
2050
- homepageContentModel
2051
- });
2052
- return {
2053
- document,
2054
- ...page.attributes ? { attributes: page.attributes } : {}
2055
- };
2056
- },
2057
- async getPages() {
2058
- const ctx = strapi2.requestContext.get();
2059
- const properties = ctx.query.properties;
2060
- const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2061
- const defaultLocale = await strapi2.plugin("i18n").service("locales").getDefaultLocale();
2062
- const settings = await strapi2.db.query(config2.uuid.app.setting).findMany({
2063
- where: {
2064
- module: "slug",
2065
- property: [
2066
- "showDefaultLanguage",
2067
- "homepageContentId",
2068
- "homepageContentModel",
2069
- "homepageSlugStrategy"
2070
- ]
2071
- }
2072
- });
2073
- const showDefaultLanguage = settings.find((setting) => setting.property === "showDefaultLanguage").value === "true";
2074
- const homepageContentId = settings.find(
2075
- (setting) => setting.property === "homepageContentId"
2076
- ).value;
2077
- const homepageContentModel = settings.find(
2078
- (setting) => setting.property === "homepageContentModel"
2079
- ).value;
2080
- const homepageSlugStrategy = settings.find(
2081
- (setting) => setting.property === "homepageSlugStrategy"
2082
- ).value;
2083
- const pages = await strapi2.db.query(config2.uuid.modules.slug).findMany({
2084
- where: {
2085
- state: "published"
2086
- }
2087
- });
2088
- pages.forEach((page) => {
2089
- if (homepageContentId === page.contentId && homepageContentModel === page.contentModel && defaultLocale === page.locale) {
2090
- page.fullSlug = "";
2091
- } else if (homepageContentId === page.contentId && homepageContentModel === page.contentModel && defaultLocale !== page.locale && homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY) {
2092
- page.fullSlug = page.locale.toLowerCase();
2093
- } else {
2094
- page.fullSlug = buildFullSlug({ pages, page, defaultLocale, showDefaultLanguage });
2101
+ if (!page) {
2102
+ ctx.status = 404;
2103
+ return;
2095
2104
  }
2096
- });
2097
- if (properties && properties.includes("attributes")) {
2098
- for (const page of pages) {
2105
+ const properties = ctx.query.properties;
2106
+ if (properties && properties.includes("attributes")) {
2099
2107
  const attributes2 = await strapi2.db.query(config2.uuid.modules.attribute).findOne({
2100
2108
  where: {
2101
2109
  locale: page.locale,
@@ -2116,140 +2124,245 @@ const SlugModuleService = ({ strapi: strapi2 }) => ({
2116
2124
  ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2117
2125
  };
2118
2126
  }
2119
- }
2120
- const mappedPages = pages.map((page) => ({
2121
- ...page,
2122
- ...page.attributes ? { attributes: page.attributes } : {},
2123
- localizations: []
2124
- }));
2125
- const rootItems = mappedPages.filter((item) => item.locale === defaultLocale);
2126
- const mappedItems = rootItems.map((rootItem) => {
2127
- const relatedItems = mappedPages.filter(
2128
- (item) => item.contentId === rootItem.contentId && item.contentModel === rootItem.contentModel && item.locale !== defaultLocale
2129
- );
2127
+ const query = populateQueryFromContentType(strapi2, page.contentModel);
2128
+ const document = await strapi2.documents(page.contentModel).findOne({
2129
+ locale: page.locale,
2130
+ populate: query,
2131
+ status: "published",
2132
+ documentId: page.contentId
2133
+ });
2134
+ await findDocumentIdsAndFetchSlugs({
2135
+ document,
2136
+ defaultLocale,
2137
+ showDefaultLanguage
2138
+ });
2139
+ for (const localization of document.localizations) {
2140
+ if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY && localization.documentId === homepageContentId) {
2141
+ localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
2142
+ } else {
2143
+ localization.slug = await getSlugByDocumentId({
2144
+ contentId: localization.documentId,
2145
+ locale: localization.locale,
2146
+ defaultLocale,
2147
+ showDefaultLanguage,
2148
+ strapi: strapi2
2149
+ });
2150
+ }
2151
+ }
2152
+ delete document.createdBy;
2153
+ delete document.updatedBy;
2154
+ document.slug = page.fullSlug;
2155
+ document.breadcrumbs = await buildBreadcrumbs({
2156
+ strapi: strapi2,
2157
+ page,
2158
+ defaultLocale,
2159
+ showDefaultLanguage,
2160
+ homepageSlugStrategy,
2161
+ homepageContentId,
2162
+ homepageContentModel
2163
+ });
2130
2164
  return {
2131
- id: rootItem.id,
2132
- documentId: rootItem.contentId,
2133
- slug: rootItem.fullSlug,
2134
- locale: rootItem.locale,
2135
- createdAt: rootItem.updatedAt,
2136
- updatedAt: rootItem.updatedAt,
2137
- publishedAt: rootItem.updatedAt,
2138
- ...rootItem.attributes ? { attributes: rootItem.attributes } : {},
2139
- localizations: relatedItems.map((item) => ({
2140
- id: item.id,
2141
- documentId: item.contentId,
2142
- slug: item.fullSlug,
2143
- locale: item.locale,
2144
- createdAt: item.updatedAt,
2145
- updatedAt: item.updatedAt,
2146
- publishedAt: item.updatedAt,
2147
- ...item.attributes ? { attributes: item.attributes } : {}
2148
- }))
2165
+ document: groupByContentTypeTabs(page.contentModel, document),
2166
+ ...page.attributes ? { attributes: page.attributes } : {}
2149
2167
  };
2150
- });
2151
- return mappedItems;
2152
- },
2153
- async getHomePage() {
2154
- const ctx = strapi2.requestContext.get();
2155
- const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2156
- const defaultLocale = await strapi2.plugin("i18n").service("locales").getDefaultLocale();
2157
- const settings = await strapi2.db.query(config2.uuid.app.setting).findMany({
2158
- where: {
2159
- module: "slug",
2160
- property: [
2161
- "showDefaultLanguage",
2162
- "homepageContentId",
2163
- "homepageContentModel",
2164
- "homepageSlugStrategy"
2165
- ]
2166
- }
2167
- });
2168
- const showDefaultLanguage = settings.find((setting) => setting.property === "showDefaultLanguage").value === "true";
2169
- const homepageContentId = settings.find(
2170
- (setting) => setting.property === "homepageContentId"
2171
- ).value;
2172
- const homepageContentModel = settings.find(
2173
- (setting) => setting.property === "homepageContentModel"
2174
- ).value;
2175
- const homepageSlugStrategy = settings.find(
2176
- (setting) => setting.property === "homepageSlugStrategy"
2177
- ).value;
2178
- const page = await strapi2.db.query(config2.uuid.modules.slug).findOne({
2179
- where: {
2180
- state: "published",
2181
- contentId: homepageContentId,
2182
- contentModel: homepageContentModel,
2183
- locale: defaultLocale
2184
- }
2185
- });
2186
- if (!page) {
2187
- ctx.status = 404;
2188
- return;
2189
- }
2190
- const properties = ctx.query.properties;
2191
- if (properties && properties.includes("attributes")) {
2192
- const attributes2 = await strapi2.db.query(config2.uuid.modules.attribute).findOne({
2168
+ },
2169
+ async getPages() {
2170
+ const ctx = strapi2.requestContext.get();
2171
+ const properties = ctx.query.properties;
2172
+ const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2173
+ const defaultLocale = await strapi2.plugin("i18n").service("locales").getDefaultLocale();
2174
+ const settings = await strapi2.db.query(config2.uuid.app.setting).findMany({
2175
+ where: {
2176
+ module: "slug",
2177
+ property: [
2178
+ "showDefaultLanguage",
2179
+ "homepageContentId",
2180
+ "homepageContentModel",
2181
+ "homepageSlugStrategy"
2182
+ ]
2183
+ }
2184
+ });
2185
+ const showDefaultLanguage = settings.find((setting) => setting.property === "showDefaultLanguage").value === "true";
2186
+ const homepageContentId = settings.find(
2187
+ (setting) => setting.property === "homepageContentId"
2188
+ ).value;
2189
+ const homepageContentModel = settings.find(
2190
+ (setting) => setting.property === "homepageContentModel"
2191
+ ).value;
2192
+ const homepageSlugStrategy = settings.find(
2193
+ (setting) => setting.property === "homepageSlugStrategy"
2194
+ ).value;
2195
+ const pages = await strapi2.db.query(config2.uuid.modules.slug).findMany({
2193
2196
  where: {
2194
- locale: page.locale,
2195
- contentId: page.contentId,
2196
- contentModel: page.contentModel,
2197
2197
  state: "published"
2198
2198
  }
2199
2199
  });
2200
- const template = attributes2 && attributes2.templateId ? await strapi2.db.query(config2.uuid.modules.template).findOne({
2200
+ pages.forEach((page) => {
2201
+ if (homepageContentId === page.contentId && homepageContentModel === page.contentModel && defaultLocale === page.locale) {
2202
+ page.fullSlug = "";
2203
+ } else if (homepageContentId === page.contentId && homepageContentModel === page.contentModel && defaultLocale !== page.locale && homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY) {
2204
+ page.fullSlug = page.locale.toLowerCase();
2205
+ } else {
2206
+ page.fullSlug = buildFullSlug({ pages, page, defaultLocale, showDefaultLanguage });
2207
+ }
2208
+ });
2209
+ if (properties && properties.includes("attributes")) {
2210
+ for (const page of pages) {
2211
+ const attributes2 = await strapi2.db.query(config2.uuid.modules.attribute).findOne({
2212
+ where: {
2213
+ locale: page.locale,
2214
+ contentId: page.contentId,
2215
+ contentModel: page.contentModel,
2216
+ state: "published"
2217
+ }
2218
+ });
2219
+ const template = attributes2 && attributes2.templateId ? await strapi2.db.query(config2.uuid.modules.template).findOne({
2220
+ where: {
2221
+ documentId: attributes2.templateId
2222
+ }
2223
+ }) : null;
2224
+ page.attributes = {
2225
+ template: template?.uid || "default",
2226
+ priority: attributes2?.priority || "0.5",
2227
+ frequency: attributes2?.frequency || "weekly",
2228
+ ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2229
+ };
2230
+ }
2231
+ }
2232
+ const mappedPages = pages.map((page) => ({
2233
+ ...page,
2234
+ ...page.attributes ? { attributes: page.attributes } : {},
2235
+ localizations: []
2236
+ }));
2237
+ const rootItems = mappedPages.filter((item) => item.locale === defaultLocale);
2238
+ const mappedItems = rootItems.map((rootItem) => {
2239
+ const relatedItems = mappedPages.filter(
2240
+ (item) => item.contentId === rootItem.contentId && item.contentModel === rootItem.contentModel && item.locale !== defaultLocale
2241
+ );
2242
+ return {
2243
+ id: rootItem.id,
2244
+ documentId: rootItem.contentId,
2245
+ slug: rootItem.fullSlug,
2246
+ locale: rootItem.locale,
2247
+ createdAt: rootItem.updatedAt,
2248
+ updatedAt: rootItem.updatedAt,
2249
+ publishedAt: rootItem.updatedAt,
2250
+ ...rootItem.attributes ? { attributes: rootItem.attributes } : {},
2251
+ localizations: relatedItems.map((item) => ({
2252
+ id: item.id,
2253
+ documentId: item.contentId,
2254
+ slug: item.fullSlug,
2255
+ locale: item.locale,
2256
+ createdAt: item.updatedAt,
2257
+ updatedAt: item.updatedAt,
2258
+ publishedAt: item.updatedAt,
2259
+ ...item.attributes ? { attributes: item.attributes } : {}
2260
+ }))
2261
+ };
2262
+ });
2263
+ return mappedItems;
2264
+ },
2265
+ async getHomePage() {
2266
+ const ctx = strapi2.requestContext.get();
2267
+ const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
2268
+ const defaultLocale = await strapi2.plugin("i18n").service("locales").getDefaultLocale();
2269
+ const settings = await strapi2.db.query(config2.uuid.app.setting).findMany({
2201
2270
  where: {
2202
- documentId: attributes2.templateId
2271
+ module: "slug",
2272
+ property: [
2273
+ "showDefaultLanguage",
2274
+ "homepageContentId",
2275
+ "homepageContentModel",
2276
+ "homepageSlugStrategy"
2277
+ ]
2203
2278
  }
2204
- }) : null;
2205
- page.attributes = {
2206
- template: template?.uid || "default",
2207
- priority: attributes2?.priority || "0.5",
2208
- frequency: attributes2?.frequency || "weekly",
2209
- ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2210
- };
2211
- }
2212
- const query = properties && properties.includes("content") ? populateQueryFromContentType(strapi2, page.contentModel) : { localizations: { populate: "*" } };
2213
- const document = await strapi2.documents(page.contentModel).findOne({
2214
- locale: page.locale,
2215
- populate: query,
2216
- status: "published",
2217
- documentId: page.contentId
2218
- });
2219
- await findDocumentIdsAndFetchSlugs({
2220
- document,
2221
- defaultLocale,
2222
- showDefaultLanguage
2223
- });
2224
- for (const localization of document.localizations) {
2225
- if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY) {
2226
- localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
2227
- } else {
2228
- localization.slug = await getSlugByDocumentId({
2229
- contentId: localization.documentId,
2230
- locale: localization.locale,
2231
- defaultLocale,
2232
- showDefaultLanguage,
2233
- strapi: strapi2
2279
+ });
2280
+ const showDefaultLanguage = settings.find((setting) => setting.property === "showDefaultLanguage").value === "true";
2281
+ const homepageContentId = settings.find(
2282
+ (setting) => setting.property === "homepageContentId"
2283
+ ).value;
2284
+ const homepageContentModel = settings.find(
2285
+ (setting) => setting.property === "homepageContentModel"
2286
+ ).value;
2287
+ const homepageSlugStrategy = settings.find(
2288
+ (setting) => setting.property === "homepageSlugStrategy"
2289
+ ).value;
2290
+ const page = await strapi2.db.query(config2.uuid.modules.slug).findOne({
2291
+ where: {
2292
+ state: "published",
2293
+ contentId: homepageContentId,
2294
+ contentModel: homepageContentModel,
2295
+ locale: defaultLocale
2296
+ }
2297
+ });
2298
+ if (!page) {
2299
+ ctx.status = 404;
2300
+ return;
2301
+ }
2302
+ const properties = ctx.query.properties;
2303
+ if (properties && properties.includes("attributes")) {
2304
+ const attributes2 = await strapi2.db.query(config2.uuid.modules.attribute).findOne({
2305
+ where: {
2306
+ locale: page.locale,
2307
+ contentId: page.contentId,
2308
+ contentModel: page.contentModel,
2309
+ state: "published"
2310
+ }
2234
2311
  });
2312
+ const template = attributes2 && attributes2.templateId ? await strapi2.db.query(config2.uuid.modules.template).findOne({
2313
+ where: {
2314
+ documentId: attributes2.templateId
2315
+ }
2316
+ }) : null;
2317
+ page.attributes = {
2318
+ template: template?.uid || "default",
2319
+ priority: attributes2?.priority || "0.5",
2320
+ frequency: attributes2?.frequency || "weekly",
2321
+ ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2322
+ };
2235
2323
  }
2236
- }
2237
- delete document.createdBy;
2238
- delete document.updatedBy;
2239
- document.slug = "";
2240
- document.breadcrumbs = [
2241
- {
2242
- position: 1,
2243
- name: document.title,
2244
- item: ""
2324
+ const query = properties && properties.includes("content") ? populateQueryFromContentType(strapi2, page.contentModel) : { localizations: { populate: "*" } };
2325
+ const document = await strapi2.documents(page.contentModel).findOne({
2326
+ locale: page.locale,
2327
+ populate: query,
2328
+ status: "published",
2329
+ documentId: page.contentId
2330
+ });
2331
+ await findDocumentIdsAndFetchSlugs({
2332
+ document,
2333
+ defaultLocale,
2334
+ showDefaultLanguage
2335
+ });
2336
+ for (const localization of document.localizations) {
2337
+ if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY) {
2338
+ localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
2339
+ } else {
2340
+ localization.slug = await getSlugByDocumentId({
2341
+ contentId: localization.documentId,
2342
+ locale: localization.locale,
2343
+ defaultLocale,
2344
+ showDefaultLanguage,
2345
+ strapi: strapi2
2346
+ });
2347
+ }
2245
2348
  }
2246
- ];
2247
- return {
2248
- document,
2249
- ...page.attributes ? { attributes: page.attributes } : {}
2250
- };
2251
- }
2252
- });
2349
+ delete document.createdBy;
2350
+ delete document.updatedBy;
2351
+ document.slug = "";
2352
+ document.breadcrumbs = [
2353
+ {
2354
+ position: 1,
2355
+ name: document.title,
2356
+ item: ""
2357
+ }
2358
+ ];
2359
+ return {
2360
+ document: groupByContentTypeTabs(page.contentModel, document),
2361
+ ...page.attributes ? { attributes: page.attributes } : {}
2362
+ };
2363
+ }
2364
+ };
2365
+ };
2253
2366
  async function fetchAdditionalData(params) {
2254
2367
  const content = await params.strapi.documents(params.record.contentModel).findOne({
2255
2368
  status: "published",