feat: simplify region update logic, per review
Some checks failed
Build and Publish Docker Image / Build and Publish Docker Image (amd64) (push) Has been cancelled
Build and Publish Docker Image / Build and Publish Docker Image (arm64) (push) Has been cancelled

This commit is contained in:
limes
2026-08-20 03:29:58 +02:00
parent 770c706c0f
commit cbf2fdf0f2
3 changed files with 16 additions and 48 deletions

8
package-lock.json generated
View File

@@ -12,7 +12,7 @@
"@aws-sdk/client-s3": "^3.657.0",
"@aws-sdk/client-ses": "^3.515.0",
"@inquirer/prompts": "^7.2.0",
"@pretendonetwork/grpc": "^2.5.6",
"@pretendonetwork/grpc": "^2.5.7",
"bcrypt": "^5.0.0",
"buffer-crc32": "^0.2.13",
"colors": "^1.4.0",
@@ -1669,9 +1669,9 @@
}
},
"node_modules/@pretendonetwork/grpc": {
"version": "2.5.6",
"resolved": "https://registry.npmjs.org/@pretendonetwork/grpc/-/grpc-2.5.6.tgz",
"integrity": "sha512-AHsWYTIJMipokrBEi6azf71i8NLsb1p3LkNJMdJ3Guc/+imlsdFSRnRoAj6K85YxE6prOgm0wsQQ5JEOFDd8Sg==",
"version": "2.5.7",
"resolved": "https://registry.npmjs.org/@pretendonetwork/grpc/-/grpc-2.5.7.tgz",
"integrity": "sha512-HmWyBxm/Om6S5k+PYLxVU/MaRcSUEV0NAfKccy4i02ZJTZccCJRRKiSBl3WfXSYE+TJNJhSoURGK56E1i8CXqA==",
"license": "AGPL-3.0-only",
"dependencies": {
"@bufbuild/protobuf": "^2.2.2",

View File

@@ -28,7 +28,7 @@
"@aws-sdk/client-s3": "^3.657.0",
"@aws-sdk/client-ses": "^3.515.0",
"@inquirer/prompts": "^7.2.0",
"@pretendonetwork/grpc": "^2.5.6",
"@pretendonetwork/grpc": "^2.5.7",
"bcrypt": "^5.0.0",
"buffer-crc32": "^0.2.13",
"colors": "^1.4.0",

View File

@@ -3,7 +3,7 @@ import Mii from 'mii-js';
import { isValidBirthday } from '@/util';
import { config } from '@/config-manager';
import timezones from '@/services/nnas/timezones.json';
import regionsList from '@/services/nnas/regions.json';
import regions from '@/services/nnas/regions.json';
import type { CallContext } from 'nice-grpc';
import type {
UpdateUserDataRequest,
@@ -23,7 +23,6 @@ export async function updateUserData(
const mii = request?.mii?.trim();
const birthday = request.birthday?.trim();
const gender = request.gender?.trim();
const country = request.country?.trim();
const region = request.region;
const timezone = request.timezone?.trim();
@@ -92,50 +91,19 @@ export async function updateUserData(
pnid.gender = gender;
}
if (country || region) {
let countryId = 0;
if (region) {
const countryObj = regions.find(c => c.id === ((region >>> 24) & 0xFF));
const regionObj = countryObj?.regions.find(r => r.id === region);
// if we have a region but no country, we extract the country id from it
if (region && !country) {
const regionHex = region.toString(16).padStart(8, '0');
countryId = parseInt(regionHex.slice(0, 2), 16);
}
const countryObj = regionsList.find((c) => {
return c.iso_code === country || c.id === countryId;
});
if (!countryObj?.iso_code) {
throw new ServerError(Status.INVALID_ARGUMENT, `Invalid country`);
}
if (region) {
const regionObj = countryObj.regions.find((r) => {
return r.id === region;
});
if (!regionObj) {
throw new ServerError(
Status.INVALID_ARGUMENT,
`Invalid region`
);
}
pnid.country = countryObj.iso_code;
pnid.region = region;
} else if (pnid.country !== countryObj?.iso_code) {
const unspecifiedRegion = countryObj.regions.find(
r => r.name === 'Unspecified'
if (!countryObj || !regionObj) {
throw new ServerError(
Status.INVALID_ARGUMENT,
`Invalid region`
);
if (!unspecifiedRegion) {
throw new ServerError(Status.INVALID_ARGUMENT, `A default region does not exist for the selected country: please set one explicitly`);
}
// if editing the country with no explicit region, set it to Unspecified
pnid.country = countryObj.iso_code;
pnid.region = unspecifiedRegion.id;
}
pnid.country = countryObj.iso_code;
pnid.region = regionObj.id;
}
if (timezone) {