Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit ff3f520

Browse files
authored
fix(PMD-94): make it work for multi presence input (#734)
1 parent dce4fdf commit ff3f520

File tree

4 files changed

+71
-41
lines changed

4 files changed

+71
-41
lines changed

master/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "PreMiD-API",
3-
"version": "4.0.0",
3+
"version": "4.2.0",
44
"main": "index.js",
55
"license": "MPL-2.0",
66
"scripts": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "PreMiD-API",
3-
"version": "4.0.1",
3+
"version": "4.2.0",
44
"main": "index.js",
55
"license": "MPL-2.0",
66
"scripts": {

worker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "PreMiD-API",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"main": "index.js",
55
"license": "MPL-2.0",
66
"scripts": {

worker/src/v4/fields/availablePresenceLanguages.ts

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,89 @@ export const schema = gql`
1212
Presence, e.g. 'Netflix'
1313
"""
1414
presence: StringOrStringArray
15-
): [Language!]!
15+
): [PresenceLanguage!]!
16+
}
17+
18+
type PresenceLanguage {
19+
"""
20+
Presence, e.g. 'Netflix'
21+
"""
22+
presence: String!
23+
"""
24+
The available languages for the presence
25+
"""
26+
languages: [Language!]!
1627
}
1728
`;
1829

1930
export async function resolver(
2031
_: any,
2132
args: {
22-
presence?: string | string[];
33+
presence: string | string[];
2334
},
2435
{ dataSources: { strings } }: { dataSources: { strings: Strings } }
2536
) {
26-
const fetchedStrings = await strings.get(args),
27-
englishStrings = fetchedStrings.find(s => s.lang === "en-US") ?? {
28-
translations: {}
29-
},
30-
// String count of the English strings by first name
31-
englishStringCount: {
32-
[key: string]: number;
33-
} = {};
37+
args.presence = Array.isArray(args.presence)
38+
? args.presence
39+
: [args.presence];
3440

35-
for (const key of Object.keys(englishStrings.translations)) {
36-
const [firstName] = key.split(".");
37-
englishStringCount[firstName] = (englishStringCount[firstName] ?? 0) + 1;
38-
}
41+
const returnObject: {
42+
[service: string]: {
43+
lang: string;
44+
nativeName: string;
45+
direction: "ltr" | "rtl";
46+
}[];
47+
} = {};
3948

40-
return fetchedStrings
41-
.filter(s => {
42-
const fetchedStringCount: {
49+
for (const presence of args.presence) {
50+
const fetchedStrings = await strings.get({ presence }),
51+
englishStrings = fetchedStrings.find(s => s.lang === "en-US") ?? {
52+
translations: {}
53+
},
54+
// String count of the English strings by first name
55+
englishStringCount: {
4356
[key: string]: number;
4457
} = {};
4558

46-
for (const key of Object.keys(s.translations)) {
47-
const [firstName] = key.split(".");
48-
fetchedStringCount[firstName] =
49-
(fetchedStringCount[firstName] ?? 0) + 1;
50-
}
59+
for (const key of Object.keys(englishStrings.translations)) {
60+
const [firstName] = key.split(".");
61+
englishStringCount[firstName] = (englishStringCount[firstName] ?? 0) + 1;
62+
}
63+
64+
returnObject[presence] = fetchedStrings
65+
.filter(s => {
66+
const fetchedStringCount: {
67+
[key: string]: number;
68+
} = {};
69+
70+
for (const key of Object.keys(s.translations)) {
71+
const [firstName] = key.split(".");
72+
fetchedStringCount[firstName] =
73+
(fetchedStringCount[firstName] ?? 0) + 1;
74+
}
5175

52-
// Check if the fetched language has 60% of the strings translated per first name
53-
for (const firstName of Object.keys(englishStringCount)) {
54-
const sixtyPercent = englishStringCount[firstName] * 0.6;
76+
// Check if the fetched language has 60% of the strings translated per first name
77+
for (const firstName of Object.keys(englishStringCount)) {
78+
const sixtyPercent = englishStringCount[firstName] * 0.6;
5579

56-
if (
57-
!(firstName in fetchedStringCount) ||
58-
fetchedStringCount[firstName] < sixtyPercent
59-
)
60-
return false;
61-
}
80+
if (
81+
!(firstName in fetchedStringCount) ||
82+
fetchedStringCount[firstName] < sixtyPercent
83+
)
84+
return false;
85+
}
86+
87+
return true;
88+
})
89+
.map(s => ({
90+
lang: s.lang,
91+
nativeName: s.nativeName,
92+
direction: s.direction
93+
}));
94+
}
6295

63-
return true;
64-
})
65-
.map(s => ({
66-
lang: s.lang,
67-
nativeName: s.nativeName,
68-
direction: s.direction
69-
}));
96+
return Object.entries(returnObject).map(([presence, languages]) => ({
97+
presence,
98+
languages
99+
}));
70100
}

0 commit comments

Comments
 (0)