diff --git a/apps/edgecreator/src/i18n.ts b/apps/edgecreator/src/i18n.ts index 6b94fd73a..f7b2ff091 100644 --- a/apps/edgecreator/src/i18n.ts +++ b/apps/edgecreator/src/i18n.ts @@ -3,10 +3,7 @@ import { createI18n } from "vue-i18n"; import fr from "../locales/fr-FR.json"; const messages = { - "en-US": Object.keys(fr).reduce( - (acc, value) => ({ ...acc, [value]: value }), - {}, - ), + "en-US": Object.fromEntries(Object.keys(fr).map(key => [key, key])), fr, }; diff --git a/apps/edgecreator/src/pages/[...all].vue b/apps/edgecreator/src/pages/[...all].vue index c9d398e0b..e2d4ce4bb 100644 --- a/apps/edgecreator/src/pages/[...all].vue +++ b/apps/edgecreator/src/pages/[...all].vue @@ -301,13 +301,7 @@ watch( const publicationcodes = [ ...mostWantedEdges.value!.map(({ issuecode }) => issuecode!), ...Object.values(currentEdges.value).map(({ issuecode }) => issuecode), - ].reduce( - (acc, issuecode) => [ - ...acc, - issuecodeDetails.value[issuecode].publicationcode, - ], - [] - ); + ].map((issuecode) => issuecodeDetails.value[issuecode].publicationcode); await coaStore.fetchPublicationNames(publicationcodes); isUploadableEdgesCarouselReady.value = true; diff --git a/apps/web/src/components/ContextMenuOnSaleByOthers.vue b/apps/web/src/components/ContextMenuOnSaleByOthers.vue index 950cb0daa..2842e0f56 100644 --- a/apps/web/src/components/ContextMenuOnSaleByOthers.vue +++ b/apps/web/src/components/ContextMenuOnSaleByOthers.vue @@ -123,19 +123,11 @@ const selectedIssues = $computed(() => Object.keys(selectedIssueIdsByIssuecode), ); const issueIds = $computed(() => - Object.values(selectedIssueIdsByIssuecode).reduce( - (acc, issues) => [...acc, ...issues.map(({ id }) => id!)], - [], - ), + Object.values(selectedIssueIdsByIssuecode).map(({ id }) => id), ); const selectedIssuesBuyerIds = $computed(() => [ - ...new Set( - issueIds.reduce( - (acc, issueId) => [...acc, issuesOnSaleById.value[issueId].userId], - [], - ), - ), + ...new Set(issueIds.map((issueId) => issuesOnSaleById.value[issueId].userId)), ]); const issuesWithMultipleCopiesSelected = $computed(() => diff --git a/apps/web/src/components/IssueList.vue b/apps/web/src/components/IssueList.vue index 2279d83c9..ddad441f7 100644 --- a/apps/web/src/components/IssueList.vue +++ b/apps/web/src/components/IssueList.vue @@ -427,10 +427,14 @@ let userIssuecodesNotFoundForPublication = $shallowRef([]); let selected = $shallowRef([]); const filteredUserCopies = $computed(() => filteredIssues.reduce<(issue & { issuecode: string })[]>( - (acc, { userCopies }) => [...acc, ...userCopies], + (acc, { userCopies }) => { + acc.push(...userCopies); + return acc; + }, [], ), ); + const copiesBySelectedIssuecode = $computed(() => selected.reduce<{ [issuecode: string]: issue[] }>((acc, issueKey) => { const [issuecode, maybeIssueId] = issueKey.split("-id-"); @@ -460,10 +464,7 @@ const showFilter = $computed( ); const issueIds = $computed(() => - Object.values(copiesBySelectedIssuecode).reduce( - (acc, issues) => [...acc, ...issues.map(({ id }) => id)], - [], - ), + Object.values(copiesBySelectedIssuecode).map(({ id }) => id), ); let contextMenuKey = $ref("context-menu"); @@ -605,31 +606,27 @@ const loadIssues = async () => { ]; issues = coaIssuecodes .filter((issuecode) => userIssuecodes.includes(issuecode)) - .reduce( - (acc, issuecode) => [ - ...acc, - ...userIssuesForPublication! - .filter( - ({ issuecode: userIssuecode }) => userIssuecode === issuecode, - ) - .map((issue) => ({ - ...issue, - key: `${issue.issuecode.replaceAll(" ", "_")}-id-${issue.id}`, - userCopies: [{ ...issue, copyIndex: 0 }], - })), - ], - [], - ); + .reduce((acc, issuecode) => { + const filteredIssues = userIssuesForPublication! + .filter( + ({ issuecode: userIssuecode }) => userIssuecode === issuecode, + ) + .map((issue) => ({ + ...issue, + key: `${issue.issuecode.replaceAll(" ", "_")}-id-${issue.id}`, + userCopies: [{ ...issue, copyIndex: 0 }], + })); + acc.push(...filteredIssues); + return acc; + }, []); } if (duplicatesOnly) { - const countPerIssuecode = issues!.reduce<{ - [issuecode: string]: number; - }>( - (acc, { userCopies }) => ({ - ...acc, - [userCopies[0].issuecode]: (acc[userCopies[0].issuecode] || 0) + 1, - }), + const countPerIssuecode = issues!.reduce<{ [issuecode: string]: number }>( + (acc, { userCopies: [{ issuecode }] }) => { + acc[issuecode] = (acc[issuecode] || 0) + 1; + return acc; + }, {}, ); issues = issues!.filter( diff --git a/apps/web/src/components/PublicationList.vue b/apps/web/src/components/PublicationList.vue index 9bbca8b8d..cc2297b19 100644 --- a/apps/web/src/components/PublicationList.vue +++ b/apps/web/src/components/PublicationList.vue @@ -99,19 +99,18 @@ const sortedCountries = $computed( ) || 0, ), ); -const publicationsPerCountry = $computed( - () => - totalPerCountry.value && - hasPublicationNames && - Object.keys(totalPerCountry.value).reduce<{ [key: string]: string[] }>( - (acc, country) => ({ - ...acc, - [country]: Object.keys(totalPerPublication.value!).filter( - (publicationcode) => publicationcode.split("/")[0] === country, - ), - }), - {}, - ), +const publicationsPerCountry = $computed(() => + !totalPerCountry.value || !hasPublicationNames + ? {} + : Object.keys(totalPerCountry.value).reduce<{ [key: string]: string[] }>( + (acc, country) => { + acc[country] = Object.keys(totalPerPublication.value!).filter( + (publicationcode) => publicationcode.split("/")[0] === country, + ); + return acc; + }, + {}, + ), ); const getSortedPublications = (country: string) => publicationsPerCountry?.[country]?.sort((a, b) => diff --git a/apps/web/src/composables/useCollection.ts b/apps/web/src/composables/useCollection.ts index bfe8cd0ab..ba3b4a3f9 100644 --- a/apps/web/src/composables/useCollection.ts +++ b/apps/web/src/composables/useCollection.ts @@ -32,23 +32,18 @@ export default (issues: ShallowRef<(issue & { issuecode: string })[]>) => { issuesByIssuecode = computed(() => issues.value?.groupBy("issuecode", "[]"), ), - duplicateIssues = computed( - (): { - [issuecode: string]: issue[]; - } => - (issuesByIssuecode.value && - Object.keys(issuesByIssuecode.value).reduce( - (acc, issuecode) => - issuesByIssuecode.value![issuecode].length > 1 - ? { - ...acc, - [issuecode]: issuesByIssuecode.value![issuecode], - } - : acc, - {}, - )) || + duplicateIssues = computed(() => { + const issues = issuesByIssuecode.value || {}; + return Object.keys(issues).reduce<{ [issuecode: string]: issue[] }>( + (acc, issuecode) => { + if (issues[issuecode].length > 1) { + acc[issuecode] = issues[issuecode]; + } + return acc; + }, {}, - ), + ); + }), issuesInToReadStack = computed(() => issues.value?.filter(({ isToRead }) => isToRead), ), @@ -69,11 +64,11 @@ export default (issues: ShallowRef<(issue & { issuecode: string })[]>) => { ), totalPerCountry = computed(() => issues.value?.reduce<{ [countrycode: string]: number }>( - (acc, issue) => ({ - ...acc, - [issue.publicationcode.split("/")[0]]: - (acc[issue.publicationcode.split("/")[0]] || 0) + 1, - }), + (acc, { publicationcode }) => { + const countrycode = publicationcode.split("/")[0]; + acc[countrycode] = (acc[countrycode] || 0) + 1; + return acc; + }, {}, ), ), diff --git a/apps/web/src/i18n.ts b/apps/web/src/i18n.ts index e265b2ea0..65b6f8190 100644 --- a/apps/web/src/i18n.ts +++ b/apps/web/src/i18n.ts @@ -9,9 +9,12 @@ export default ( if (!instance) { const messages = { ...translations, - [defaultLocale]: Object.keys( - translations[Object.keys(translations)[0]], - ).reduce((acc, value) => ({ ...acc, [value]: value }), {}), + [defaultLocale]: Object.fromEntries( + Object.keys(translations[Object.keys(translations)[0]]).map((key) => [ + key, + key, + ]), + ), }; const fallbackLocale = defaultLocale; diff --git a/apps/web/src/pages/admin/edges/progress.vue b/apps/web/src/pages/admin/edges/progress.vue index a97c82b73..e55622c7e 100644 --- a/apps/web/src/pages/admin/edges/progress.vue +++ b/apps/web/src/pages/admin/edges/progress.vue @@ -160,46 +160,38 @@ const open = (inducksIssuecode: string) => { const inducksIssuenumbers = $computed(() => Object.keys(issuecodesByPublicationcode.value).reduce< Record - >( - (acc, publicationcode) => ({ - ...acc, - [publicationcode]: Object.values( - issuecodesByPublicationcode.value[publicationcode], - ).map((issuecode) => - issuecodeDetails.value[issuecode].issuenumber.replaceAll(" ", ""), - ), - }), - {}, - ), + >((acc, publicationcode) => { + acc[publicationcode] = Object.values( + issuecodesByPublicationcode.value[publicationcode], + ).map((issuecode) => + issuecodeDetails.value[issuecode].issuenumber.replaceAll(" ", ""), + ); + return acc; + }, {}), ); const sortedBookcase = computed(() => Object.values(showEdgesForPublication).reduce< Record - >( - (acc, publicationcode) => ({ - ...acc, - [publicationcode]: - issuecodesByPublicationcode.value[publicationcode]?.map( - (issuecode) => ({ - id: 0, - edgeId: publishedEdgesByPublicationcode?.[publicationcode] - .map(({ issuecode }) => issuecode) - .includes(issuecode) - ? 1 - : 0, - publicationcode, - issuecode, - creationDate: new Date(), - sprites: [], - points: 0, - slug: "", - timestamp: new Date().getTime(), - }), - ) || [], - }), - {}, - ), + >((acc, publicationcode) => { + acc[publicationcode] = + issuecodesByPublicationcode.value[publicationcode]?.map((issuecode) => ({ + id: 0, + edgeId: publishedEdgesByPublicationcode?.[publicationcode] + .map(({ issuecode }) => issuecode) + .includes(issuecode) + ? 1 + : 0, + publicationcode, + issuecode, + creationDate: new Date(), + sprites: [], + points: 0, + slug: "", + timestamp: new Date().getTime(), + })) || []; + return acc; + }, {}), ); (async () => { diff --git a/apps/web/src/pages/print/classic.vue b/apps/web/src/pages/print/classic.vue index 18179b103..25dc9218b 100644 --- a/apps/web/src/pages/print/classic.vue +++ b/apps/web/src/pages/print/classic.vue @@ -91,18 +91,18 @@ watch( ); ownedIssuecodes = Object.entries( issuecodesByPublicationcode.value, - ).reduce( - (acc, [publicationcode, indexedIssuecodes]) => ({ - ...acc, - [publicationcode]: indexedIssuecodes + ).reduce>( + (acc, [publicationcode, indexedIssuecodes]) => { + acc[publicationcode] = indexedIssuecodes .filter((indexedIssuecode) => collectionWithPublicationcodes[publicationcode].some( ({ issuecode }) => issuecode === indexedIssuecode, ), ) .map((issuecode) => issuecodeDetails.value[issuecode].issuenumber) - .join(", "), - }), + .join(", "); + return acc; + }, {}, ); } diff --git a/apps/web/src/pages/stats/publications.vue b/apps/web/src/pages/stats/publications.vue index 6ffd1f723..ad391fa3e 100644 --- a/apps/web/src/pages/stats/publications.vue +++ b/apps/web/src/pages/stats/publications.vue @@ -47,13 +47,10 @@ const totalPerPublicationGroupSmallCounts: { (publicationcode) => !smallCountPublications.includes(publicationcode), ) - .reduce( - (acc, publicationcode) => ({ - ...acc, - [publicationcode]: totalPerPublication.value![publicationcode], - }), - {}, - ), + .reduce>((acc, publicationcode) => { + acc[publicationcode] = totalPerPublication.value![publicationcode]; + return acc; + }, {}), ...(!smallCountPublications.length ? {} : { @@ -66,22 +63,22 @@ const totalPerPublicationGroupSmallCounts: { }) || {}, ); -const labels = $computed( - () => - hasPublicationNames && - Object.entries(totalPerPublicationGroupSmallCounts) - .sort(sortByCount) - .reduce( - (acc, [publicationcode]) => [ - ...acc, - publicationNames.value[publicationcode] || - `${$t("Autres")} (${smallCountPublications!.length} ${$t( - "Publications", - ).toLowerCase()})`, - ], - [], - ), -); +const labels = $computed(() => { + if (!hasPublicationNames) return false; + + return Object.entries(totalPerPublicationGroupSmallCounts) + .sort(sortByCount) + .reduce((acc, [publicationcode]) => { + acc.push( + publicationNames.value[publicationcode] || + `${$t("Autres")} (${smallCountPublications!.length} ${$t( + "Publications", + ).toLowerCase()})`, + ); + return acc; + }, []); +}); + const values = $computed(() => Object.values(totalPerPublicationGroupSmallCounts).sort((count1, count2) => Math.sign(count1 - count2), diff --git a/apps/web/src/stores/coa.ts b/apps/web/src/stores/coa.ts index 6f1c7017b..f9b41e04e 100644 --- a/apps/web/src/stores/coa.ts +++ b/apps/web/src/stores/coa.ts @@ -9,21 +9,21 @@ import { socketInjectionKey } from "../composables/useDmSocket"; const addPartInfo = (issueDetails: InducksIssueDetails) => { const storyPartCounter = Object.entries( issueDetails.entries.reduce<{ [storycode: string]: number }>( - (acc, { storycode }) => ({ - ...acc, - [storycode]: !storycode ? 0 : (acc[storycode] || 0) + 1, - }), + (acc, { storycode }) => { + if (storycode) { + acc[storycode] = (acc[storycode] || 0) + 1; + } + return acc; + }, {}, ), ) .filter(([, occurrences]) => occurrences > 1) - .reduce<{ [storycode: string]: number }>( - (acc, [storycode]) => ({ - ...acc, - [storycode]: 1, - }), - {}, - ); + .reduce<{ [storycode: string]: number }>((acc, [storycode]) => { + acc[storycode] = 1; + return acc; + }, {}); + return { ...issueDetails, entries: issueDetails.entries.map((entry) => ({ diff --git a/apps/web/src/stores/collection.ts b/apps/web/src/stores/collection.ts index 817c69158..8c5231ca8 100644 --- a/apps/web/src/stores/collection.ts +++ b/apps/web/src/stores/collection.ts @@ -99,20 +99,15 @@ export const collection = defineStore("collection", () => { issuecodesPerPublication = computed( () => issues.value?.groupBy("publicationcode", "[]") || {}, ), - totalPerPublicationUniqueIssuecodes = computed( - (): { - [publicationcode: string]: number; - } => - issuecodesPerPublication.value && - Object.keys(issuecodesPerPublication.value).reduce( - (acc, publicationcode) => ({ - ...acc, - [publicationcode]: [ - ...new Set(issuecodesPerPublication.value[publicationcode]), - ].length, - }), - {}, + totalPerPublicationUniqueIssuecodes = computed(() => + Object.fromEntries( + Object.entries(issuecodesPerPublication.value || {}).map( + ([publicationcode, issuecodes]) => [ + publicationcode, + new Set(issuecodes).size, + ], ), + ), ), totalPerPublicationUniqueIssuecodesSorted = computed( () => diff --git a/apps/whattheduck/src/components/PurchaseGraph.vue b/apps/whattheduck/src/components/PurchaseGraph.vue index 3f7edebf7..311740ad7 100644 --- a/apps/whattheduck/src/components/PurchaseGraph.vue +++ b/apps/whattheduck/src/components/PurchaseGraph.vue @@ -92,8 +92,8 @@ const compareDates = (a: string, b: string) => }), {}, ); + let accDate = Object.fromEntries(labels.value!.map((value) => [value, 0])); - let accDate = labels.value!.reduce>((acc, value) => ({ ...acc, [value]: 0 }), {}); return collectionWithDates.value .sort(({ date: dateA }, { date: dateB }) => compareDates(dateA, dateB)) .reduce>>((acc, { date, publicationcode: publicationcode }) => { @@ -113,15 +113,11 @@ const compareDates = (a: string, b: string) => ? null : Object.keys(values.value).map((publicationcode) => { let data = values.value![publicationcode]; - data = labels.value!.reduce( - (acc, currentDate) => ({ - ...acc, - [currentDate]: labels - .value!.filter((_, idx) => idx <= labels.value!.indexOf(currentDate)) - .reduce((sum, date) => sum + data[date], 0), - }), - {}, - ); + data = labels.value!.reduce>((acc, currentDate, idx) => { + acc[currentDate] = (acc[labels.value![idx - 1]] || 0) + data[currentDate]; + return acc; + }, {}); + return { data: Object.values(data), label: diff --git a/apps/whattheduck/src/views/IssueList.vue b/apps/whattheduck/src/views/IssueList.vue index 0da9aa3cc..568d72f9f 100644 --- a/apps/whattheduck/src/views/IssueList.vue +++ b/apps/whattheduck/src/views/IssueList.vue @@ -130,35 +130,33 @@ type Item = | (Pick & { issuecode: string }) | IssueWithIssuecodeOnly; -const items = computed(() => - coaIssuecodes.value - ? isCoaView.value - ? coaIssuecodes.value.reduce< - { - key: string; - item: Item; - }[] - >((acc, issuecode) => { - const userIssuesForThisIssue = userIssues.value.filter( - ({ issuecode: userIssuecode }) => issuecode === userIssuecode, - ); - - return [ - ...acc, - ...(userIssuesForThisIssue.length ? userIssuesForThisIssue : [issuecodeDetails.value[issuecode]]).map( - (itemOrUserItem) => ({ - key: issuecode, - item: itemOrUserItem, - }), - ), - ]; - }, []) - : (userIssues.value || []).map((issue) => ({ - key: issue.issuecode, - item: issue, - })) - : [], -); +const items = computed(() => { + if (!coaIssuecodes.value) return []; + + if (isCoaView.value) { + return coaIssuecodes.value.reduce<{ key: string; item: Item }[]>((acc, issuecode) => { + const userIssuesForThisIssue = userIssues.value.filter( + ({ issuecode: userIssuecode }) => issuecode === userIssuecode, + ); + + const itemsToAdd = userIssuesForThisIssue.length ? userIssuesForThisIssue : [issuecodeDetails.value[issuecode]]; + + for (const itemOrUserItem of itemsToAdd) { + acc.push({ + key: issuecode, + item: itemOrUserItem, + }); + } + + return acc; + }, []); + } else { + return (userIssues.value || []).map((issue) => ({ + key: issue.issuecode, + item: issue, + })); + } +}); const sortedItems = computed(() => items.value diff --git a/packages/api/emails/email.ts b/packages/api/emails/email.ts index b5dec2a6a..50abf2b9d 100644 --- a/packages/api/emails/email.ts +++ b/packages/api/emails/email.ts @@ -7,19 +7,13 @@ import type Mail from "nodemailer/lib/mailer"; import path from "path"; import en from "~/translations/messages.en.json"; -const fr = Object.keys(en).reduce((acc, key) => ({ ...acc, [key]: key }), {}); +const fr = Object.fromEntries(Object.keys(en).map((key) => [key, key])); + export const i18n = new I18n({ locales: ["fr", "en-US"], - defaultLocale: "fr", staticCatalog: { - "en-US": Object.entries(en).reduce( - (acc, [key, value]) => ({ - ...acc, - [key]: value, - }), - {}, - ), + "en-US": Object.fromEntries(Object.entries(en)), fr, }, }); diff --git a/packages/api/services/coa/authors/index.ts b/packages/api/services/coa/authors/index.ts index c4916af47..61fcac4a5 100644 --- a/packages/api/services/coa/authors/index.ts +++ b/packages/api/services/coa/authors/index.ts @@ -19,13 +19,7 @@ export default (socket: Socket) => { }); callback( - authors.reduce( - (acc, value) => ({ - ...acc, - [value.personcode]: value.fullname, - }), - {}, - ), + authors.groupBy('personcode', 'fullname') ); }); }; @@ -41,10 +35,4 @@ export const getAuthorFullNames = async ( }, }, }) - ).reduce( - (acc, value) => ({ - ...acc, - [value.personcode]: value.fullname, - }), - {}, - ); + ).groupBy('personcode', 'fullname') diff --git a/packages/api/services/coa/countries/index.ts b/packages/api/services/coa/countries/index.ts index 5a26fc615..315cb8161 100644 --- a/packages/api/services/coa/countries/index.ts +++ b/packages/api/services/coa/countries/index.ts @@ -30,18 +30,13 @@ const getCountryNames = async ( FROM inducks_country LEFT JOIN inducks_countryname on inducks_country.countrycode = inducks_countryname.countrycode WHERE languagecode = '${locale}' - AND ${ - countryIds?.length - ? `inducks_country.countrycode IN (${Prisma.join(countryIds)})` - : `inducks_country.countrycode != 'zz'` - }`, + AND ${countryIds?.length + ? `inducks_country.countrycode IN (${Prisma.join(countryIds)})` + : `inducks_country.countrycode != 'zz'` + }`, ) .then((results) => - results.reduce( - (acc, value) => ({ - ...acc, - [value.countrycode]: value.countryname || value.default_countryname, - }), - {}, - ), + Object.fromEntries( + results.map((value) => [value.countrycode, value.countryname || value.default_countryname]) + ) ); diff --git a/packages/api/services/coa/index.ts b/packages/api/services/coa/index.ts index 1c62f12b0..d3e4db53f 100644 --- a/packages/api/services/coa/index.ts +++ b/packages/api/services/coa/index.ts @@ -49,13 +49,12 @@ export const augmentIssueObjectWithInducksData = < withTitle: boolean = false, ) => getInducksIssueData(Object.keys(issues), withTitle).then((inducksIssues) => - Object.entries(issues).reduce( - (acc, [issuecode, issue]) => ({ - ...acc, - [issuecode]: { ...issue, ...inducksIssues[issuecode] }, - }), - {}, - ), + Object.fromEntries( + Object.entries(issues).map(([issuecode, issue]) => [ + issuecode, + { ...issue, ...inducksIssues[issuecode] }, + ]) + ) ); export const augmentIssueArrayWithInducksData = async < diff --git a/packages/api/services/coa/publications/index.ts b/packages/api/services/coa/publications/index.ts index 5a17c2760..3540fc26e 100644 --- a/packages/api/services/coa/publications/index.ts +++ b/packages/api/services/coa/publications/index.ts @@ -35,8 +35,5 @@ export const getPublicationTitles = async ( where: filter, }) .then((results) => - results.reduce( - (acc, value) => ({ ...acc, [value.publicationcode]: value.title }), - {}, - ), + results.groupBy('publicationcode', 'title') ); diff --git a/packages/api/services/coa/quotations/index.ts b/packages/api/services/coa/quotations/index.ts index f38273b7e..e7d79b599 100644 --- a/packages/api/services/coa/quotations/index.ts +++ b/packages/api/services/coa/quotations/index.ts @@ -10,31 +10,26 @@ const ISSUE_CODE_REGEX = /[a-z]+\/[-A-Z0-9 ]+/g; export const getQuotations = async ( filter: PrismaCoa.inducks_issuequotationWhereInput, -) => - Object.entries( - await prismaCoa.inducks_issuequotation - .findMany({ - where: filter, - }) - .then((results) => results.groupBy("issuecode")), - ).reduce>( - (acc, [issuecode, quotation]) => ({ - ...acc, - [issuecode]: { +) => { + const results = await prismaCoa.inducks_issuequotation.findMany({ + where: filter, + }); + + return results.reduce>((acc, quotation) => { + const issuecode = quotation.issuecode; + if (!acc[issuecode]) { + acc[issuecode] = quotation; + } else { + acc[issuecode] = { ...acc[issuecode], ...quotation, - estimationMin: - acc[issuecode]?.estimationMin && quotation.estimationMin - ? Math.min(acc[issuecode].estimationMin, quotation.estimationMin) - : quotation.estimationMin, - estimationMax: - acc[issuecode]?.estimationMax && quotation.estimationMax - ? Math.max(acc[issuecode].estimationMax, quotation.estimationMax) - : quotation.estimationMax, - }, - }), - {}, - ); + estimationMin: Math.min(acc[issuecode].estimationMin ?? Infinity, quotation.estimationMin ?? Infinity), + estimationMax: Math.max(acc[issuecode].estimationMax ?? -Infinity, quotation.estimationMax ?? -Infinity), + }; + } + return acc; + }, {}); +} export const getQuotationsByIssuecodes = async (issuecodes: string[]) => getQuotations({ diff --git a/packages/api/services/collection/issues/index.ts b/packages/api/services/collection/issues/index.ts index 97b099a91..1e124c5e0 100644 --- a/packages/api/services/collection/issues/index.ts +++ b/packages/api/services/collection/issues/index.ts @@ -179,15 +179,11 @@ export default (socket: Socket) => { by: ["publicationcode"], }) .then((data) => - data.reduce<{ [countrycode: string]: number }>( - (acc, { publicationcode, _count }) => { + Object.fromEntries( + data.map(({ publicationcode, _count }) => { const countrycode = publicationcode!.split("/")[0]; - return { - ...acc, - [countrycode!]: (acc[countrycode] || 0) + _count.issuenumber, - }; - }, - {}, + return [countrycode, (_count.issuenumber + (acc[countrycode] || 0))]; + }) ), ) .then(callback), @@ -208,13 +204,9 @@ export default (socket: Socket) => { }) .then((data) => { callback( - data.reduce<{ [publicationcode: string]: number }>( - (acc, { publicationcode, _count }) => ({ - ...acc, - [publicationcode!]: _count.issuenumber, - }), - {}, - ), + Object.fromEntries( + data.map(({ publicationcode, _count }) => [publicationcode!, _count.issuenumber]) + ) ); }), ); diff --git a/packages/api/services/collection/util.ts b/packages/api/services/collection/util.ts index 70458dfc9..386416450 100644 --- a/packages/api/services/collection/util.ts +++ b/packages/api/services/collection/util.ts @@ -28,15 +28,8 @@ export const getMedalPoints = async (userIds: number[]) => ON contributionType.contribution = userContributions.contribution AND userIds.userId = userContributions.userId ` - ).reduce<{ - [userId: number]: Record; - }>( - (acc, { userContributionTypeEn, totalPoints, userId }) => ({ - ...acc, - [userId]: { - ...(acc[userId] || {}), - [userContributionTypeEn]: parseInt(totalPoints) || 0, - }, - }), - {}, - ); + ).reduce<{ [userId: number]: Record }>((acc, { userContributionTypeEn, totalPoints, userId }) => { + acc[userId] = acc[userId] || {}; + acc[userId][userContributionTypeEn] = parseInt(totalPoints) || 0; + return acc; + }, {}); diff --git a/packages/api/services/edgecreator/edge-publication/index.ts b/packages/api/services/edgecreator/edge-publication/index.ts index f6cbd6b1a..7fa596354 100644 --- a/packages/api/services/edgecreator/edge-publication/index.ts +++ b/packages/api/services/edgecreator/edge-publication/index.ts @@ -71,13 +71,7 @@ const getUserIdsByUsername = async ( username: { in: usernames }, }, }) - ).reduce( - (acc, value) => ({ - ...acc, - [value.username]: value.id, - }), - {}, - ); + ).groupBy('username', 'id'); const createContribution = async ( user: user, diff --git a/packages/api/services/edgecreator/models/index.ts b/packages/api/services/edgecreator/models/index.ts index 0aa4fe075..18b3826fc 100644 --- a/packages/api/services/edgecreator/models/index.ts +++ b/packages/api/services/edgecreator/models/index.ts @@ -31,29 +31,21 @@ export default (socket: Socket) => { group by model.numero, optionValue.ordre order by optionValue.ordre ` - ).reduce( - ( - acc: ModelSteps, - { issuenumber, stepNumber, functionName, options }, - ) => ({ - ...acc, - [issuenumber]: { - ...(acc[issuenumber] || {}), - [stepNumber]: { - ...(acc[issuenumber]?.[stepNumber] || { - functionName, - issuenumber, - stepNumber, - options: { - ...(acc[issuenumber]?.[stepNumber]?.options || {}), - ...JSON.parse(options), - }, - }), - }, - }, - }), - {}, - ), + ).reduce((acc, { issuenumber, stepNumber, functionName, options }) => { + if (!acc[issuenumber]) { + acc[issuenumber] = {}; + } + if (!acc[issuenumber][stepNumber]) { + acc[issuenumber][stepNumber] = { + functionName, + issuenumber, + stepNumber, + options: {}, + }; + } + Object.assign(acc[issuenumber][stepNumber].options, JSON.parse(options)); + return acc; + }, {}), ); }); diff --git a/packages/api/services/edges/index.ts b/packages/api/services/edges/index.ts index db315dc98..f3a27c943 100644 --- a/packages/api/services/edges/index.ts +++ b/packages/api/services/edges/index.ts @@ -17,8 +17,8 @@ const getEdges = async (filters: { } const issuecode = filters.issuecodes ? { - in: filters.issuecodes, - } + in: filters.issuecodes, + } : undefined; const edgeModels: Record = ( await prismaEdgeCreator.edgeModel.findMany({ @@ -38,17 +38,14 @@ const getEdges = async (filters: { issuecode, }, }) - ).reduce( - (acc, edge) => ({ - ...acc, - [edge.issuecode!]: { - ...edge, - modelId: edgeModels[edge.issuecode]?.id, - v3: edgeModels[edge.issuecode] !== undefined, - }, - }), - {}, - ); + ).reduce((acc, edge) => { + acc[edge.issuecode!] = { + ...edge, + modelId: edgeModels[edge.issuecode]?.id, + v3: edgeModels[edge.issuecode] !== undefined, + }; + return acc; + }, {}); }; export default (io: Server) => { diff --git a/packages/api/services/global-stats/index.ts b/packages/api/services/global-stats/index.ts index 0895397d1..d17e8bad9 100644 --- a/packages/api/services/global-stats/index.ts +++ b/packages/api/services/global-stats/index.ts @@ -139,20 +139,13 @@ const getUsersQuickStats = async (userIds: number[]) => const numberOfCountriesAndPublicationsPerUser = usersAndNumberOfCountriesAndPublications.groupBy("userId"); - return counts.reduce( - (acc, { userId, _count }) => ({ - ...acc, - [userId]: { - ...usersById[userId], - numberOfCountries: - numberOfCountriesAndPublicationsPerUser[userId] - ?.numberOfCountries || 0, - numberOfPublications: - numberOfCountriesAndPublicationsPerUser[userId] - ?.numberOfPublications || 0, - numberOfIssues: _count.id, - }, - }), - {}, - ); + return counts.reduce((acc, { userId, _count }) => { + acc[userId] = { + ...usersById[userId], + numberOfCountries: numberOfCountriesAndPublicationsPerUser[userId]?.numberOfCountries || 0, + numberOfPublications: numberOfCountriesAndPublicationsPerUser[userId]?.numberOfPublications || 0, + numberOfIssues: _count.id, + }; + return acc; + }, {}); }); diff --git a/packages/api/services/stats/suggestions.ts b/packages/api/services/stats/suggestions.ts index 5f60cacb6..d358783ec 100644 --- a/packages/api/services/stats/suggestions.ts +++ b/packages/api/services/stats/suggestions.ts @@ -24,7 +24,7 @@ export default (socket: Socket) => { const since = sincePreviousVisit === "since_previous_visit" ? (await prismaDm.user.findUnique({ where: { id: user!.id } }))! - .previousAccess + .previousAccess : null; const results: Partial[0]> = {}; @@ -65,7 +65,7 @@ type MissingPublications = { interface Suggestion extends PrismaDmStats.missingIssueForUserGetPayload, - PrismaDmStats.suggestedIssueForUserGetPayload {} + PrismaDmStats.suggestedIssueForUserGetPayload { } const getStoryDetails = async ( storyCodes: string[], @@ -75,7 +75,7 @@ const getStoryDetails = async ( return {}; } - const stories = ( + const stories = Object.fromEntries(( await prismaCoa.$queryRawUnsafe(` SELECT story.storycode, story.storycomment, @@ -86,16 +86,14 @@ const getStoryDetails = async ( INNER JOIN inducks_entry entry USING (storyversioncode) INNER JOIN inducks_issue issue USING (issuecode) WHERE ${storyCodes - .map( - (storyCode, idx) => - `story.storycode = '${storyCode}' AND issue.issuecode = '${associatedIssuecodes[idx]}'`, - ) - .join(" OR ")} + .map( + (storyCode, idx) => + `story.storycode = '${storyCode}' AND issue.issuecode = '${associatedIssuecodes[idx]}'`, + ) + .join(" OR ")} ORDER BY story.storycode `) - ).reduce((acc, story) => ({ ...acc, [story.storycode]: story }), {}) as { - [storycode: string]: StoryDetail; - }; + ).map((story) => [story.storycode, story])); for (const storycode of storyCodes) { if (!stories[storycode]) { @@ -143,17 +141,15 @@ export const getSuggestions = async ( INNER JOIN utilisateurs_publications_manquantes as missing USING (ID_User, issuecode) WHERE suggested.oldestdate <= '${new Date().toISOString().split("T")[0]}' - AND (${ - since - ? `suggested.oldestdate > '${since.toISOString().split("T")[0]}'` - : "1=1" - }) + AND (${since + ? `suggested.oldestdate > '${since.toISOString().split("T")[0]}'` + : "1=1" + }) AND (${singleUserId ? `suggested.ID_User = ${singleUserId}` : "1=1"}) - AND (${ - singleCountry - ? `suggested.issuecode LIKE '${singleCountry}/%'` - : "1=1" - }) + AND (${singleCountry + ? `suggested.issuecode LIKE '${singleCountry}/%'` + : "1=1" + }) ORDER BY ID_User, ${sort} DESC, issuecode LIMIT 50 `); @@ -165,8 +161,8 @@ export const getSuggestions = async ( const countriesToNotifyPerUser = countrycode === COUNTRY_CODE_OPTION.countries_to_notify ? await getOptionValueAllUsers( - userOptionType.suggestion_notification_country, - ) + userOptionType.suggestion_notification_country, + ) : null; const suggestionsPerUser: { [userId: number]: IssueSuggestionList } = {}; @@ -229,10 +225,7 @@ export const getSuggestions = async ( }, }, }) - ).reduce( - (acc, value) => ({ ...acc, [value.personcode]: value.fullname }), - {}, - ) as { [personcode: string]: string }; + ).groupBy('personcode', 'fullname') let storyDetails: { [storycode: string]: StoryDetail } = {}; if (withStoryDetails) { @@ -260,8 +253,8 @@ const isSuggestionInCountriesToNotify = ( : !countriesToNotify[userId] ? false : countriesToNotify[userId].some((countryToNotify) => - suggestion.issuecode?.startsWith(`${countryToNotify}/`), - ); + suggestion.issuecode?.startsWith(`${countryToNotify}/`), + ); const getOptionValueAllUsers = async (optionName: userOptionType) => (