Skip to content

Commit

Permalink
fix(PMD-94): make it work for multi presence input (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas950 authored Dec 4, 2022
1 parent dce4fdf commit ff3f520
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 41 deletions.
2 changes: 1 addition & 1 deletion master/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PreMiD-API",
"version": "4.0.0",
"version": "4.2.0",
"main": "index.js",
"license": "MPL-2.0",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PreMiD-API",
"version": "4.0.1",
"version": "4.2.0",
"main": "index.js",
"license": "MPL-2.0",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion worker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PreMiD-API",
"version": "4.1.0",
"version": "4.2.0",
"main": "index.js",
"license": "MPL-2.0",
"scripts": {
Expand Down
106 changes: 68 additions & 38 deletions worker/src/v4/fields/availablePresenceLanguages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,89 @@ export const schema = gql`
Presence, e.g. 'Netflix'
"""
presence: StringOrStringArray
): [Language!]!
): [PresenceLanguage!]!
}
type PresenceLanguage {
"""
Presence, e.g. 'Netflix'
"""
presence: String!
"""
The available languages for the presence
"""
languages: [Language!]!
}
`;

export async function resolver(
_: any,
args: {
presence?: string | string[];
presence: string | string[];
},
{ dataSources: { strings } }: { dataSources: { strings: Strings } }
) {
const fetchedStrings = await strings.get(args),
englishStrings = fetchedStrings.find(s => s.lang === "en-US") ?? {
translations: {}
},
// String count of the English strings by first name
englishStringCount: {
[key: string]: number;
} = {};
args.presence = Array.isArray(args.presence)
? args.presence
: [args.presence];

for (const key of Object.keys(englishStrings.translations)) {
const [firstName] = key.split(".");
englishStringCount[firstName] = (englishStringCount[firstName] ?? 0) + 1;
}
const returnObject: {
[service: string]: {
lang: string;
nativeName: string;
direction: "ltr" | "rtl";
}[];
} = {};

return fetchedStrings
.filter(s => {
const fetchedStringCount: {
for (const presence of args.presence) {
const fetchedStrings = await strings.get({ presence }),
englishStrings = fetchedStrings.find(s => s.lang === "en-US") ?? {
translations: {}
},
// String count of the English strings by first name
englishStringCount: {
[key: string]: number;
} = {};

for (const key of Object.keys(s.translations)) {
const [firstName] = key.split(".");
fetchedStringCount[firstName] =
(fetchedStringCount[firstName] ?? 0) + 1;
}
for (const key of Object.keys(englishStrings.translations)) {
const [firstName] = key.split(".");
englishStringCount[firstName] = (englishStringCount[firstName] ?? 0) + 1;
}

returnObject[presence] = fetchedStrings
.filter(s => {
const fetchedStringCount: {
[key: string]: number;
} = {};

for (const key of Object.keys(s.translations)) {
const [firstName] = key.split(".");
fetchedStringCount[firstName] =
(fetchedStringCount[firstName] ?? 0) + 1;
}

// Check if the fetched language has 60% of the strings translated per first name
for (const firstName of Object.keys(englishStringCount)) {
const sixtyPercent = englishStringCount[firstName] * 0.6;
// Check if the fetched language has 60% of the strings translated per first name
for (const firstName of Object.keys(englishStringCount)) {
const sixtyPercent = englishStringCount[firstName] * 0.6;

if (
!(firstName in fetchedStringCount) ||
fetchedStringCount[firstName] < sixtyPercent
)
return false;
}
if (
!(firstName in fetchedStringCount) ||
fetchedStringCount[firstName] < sixtyPercent
)
return false;
}

return true;
})
.map(s => ({
lang: s.lang,
nativeName: s.nativeName,
direction: s.direction
}));
}

return true;
})
.map(s => ({
lang: s.lang,
nativeName: s.nativeName,
direction: s.direction
}));
return Object.entries(returnObject).map(([presence, languages]) => ({
presence,
languages
}));
}

0 comments on commit ff3f520

Please sign in to comment.