mirror of
https://github.com/PretendoNetwork/account.git
synced 2026-08-20 01:34:05 -05:00
Make user data update endpoint do something
This commit is contained in:
11
package-lock.json
generated
11
package-lock.json
generated
@@ -41,7 +41,8 @@
|
||||
"typescript-is": "^0.19.0",
|
||||
"validator": "^13.7.0",
|
||||
"xmlbuilder": "^13.0.2",
|
||||
"xmlbuilder2": "0.0.4"
|
||||
"xmlbuilder2": "0.0.4",
|
||||
"zod": "^3.21.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@hcaptcha/types": "^1.0.3",
|
||||
@@ -5811,6 +5812,14 @@
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/zod": {
|
||||
"version": "3.21.4",
|
||||
"resolved": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz",
|
||||
"integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,8 @@
|
||||
"typescript-is": "^0.19.0",
|
||||
"validator": "^13.7.0",
|
||||
"xmlbuilder": "^13.0.2",
|
||||
"xmlbuilder2": "0.0.4"
|
||||
"xmlbuilder2": "0.0.4",
|
||||
"zod": "^3.21.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@hcaptcha/types": "^1.0.3",
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import express from 'express';
|
||||
import joi from 'joi';
|
||||
//import { PNID } from '@/models/pnid';
|
||||
import { z } from 'zod';
|
||||
import Mii from 'mii-js';
|
||||
import { config } from '@/config-manager';
|
||||
import { PNID } from '@/models/pnid';
|
||||
import { HydratedPNIDDocument } from '@/types/mongoose/pnid';
|
||||
import { UpdateUserRequest } from '@/types/services/api/update-user-request';
|
||||
|
||||
const router: express.Router = express.Router();
|
||||
|
||||
// TODO: Extend this later with more settings
|
||||
const userSchema: joi.ObjectSchema = joi.object({
|
||||
mii: joi.object({
|
||||
name: joi.string(),
|
||||
primary: joi.string(),
|
||||
data: joi.string(),
|
||||
})
|
||||
// TODO - Extend this later with more settings
|
||||
const userSchema = z.object({
|
||||
mii: z.object({
|
||||
name: z.string().trim(),
|
||||
primary: z.enum(['Y', 'N']),
|
||||
data: z.string(),
|
||||
}).optional(),
|
||||
environment: z.enum(['prod', 'test', 'dev']).optional()
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -85,25 +87,112 @@ router.post('/', async (request: express.Request, response: express.Response): P
|
||||
return;
|
||||
}
|
||||
|
||||
const valid: joi.ValidationResult = userSchema.validate(updateUserRequest);
|
||||
const result = userSchema.safeParse(updateUserRequest);
|
||||
|
||||
if (valid.error) {
|
||||
if (!result.success) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: valid.error
|
||||
error: result.error
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO - Make this do something
|
||||
if (result.data.mii) {
|
||||
const miiNameBuffer: Buffer = Buffer.from(result.data.mii.name, 'utf16le'); // * UTF8 to UTF16
|
||||
|
||||
//const pid: number = pnid.pid;
|
||||
if (miiNameBuffer.length < 1) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Mii name too short'
|
||||
});
|
||||
|
||||
//const updateData = {};
|
||||
return;
|
||||
}
|
||||
|
||||
//await PNID.updateOne({ pid }, { $set: updateData }).exec();
|
||||
if (miiNameBuffer.length > 0x14) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Mii name too long'
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const miiDataBuffer: Buffer = Buffer.from(result.data.mii.data, 'base64');
|
||||
|
||||
if (miiDataBuffer.length < 0x60) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Mii data too short'
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (miiDataBuffer.length > 0x60) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Mii data too long'
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const mii: Mii = new Mii(miiDataBuffer);
|
||||
mii.validate();
|
||||
} catch (_) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Failed to decode Mii data'
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await pnid.updateMii({
|
||||
name: result.data.mii.name,
|
||||
primary: result.data.mii.primary,
|
||||
data: result.data.mii.data
|
||||
});
|
||||
}
|
||||
|
||||
const updateData: Record<string, any> = {};
|
||||
|
||||
if (result.data.environment) {
|
||||
const environment: string = result.data.environment;
|
||||
|
||||
if (environment === 'test' && pnid.access_level < 1) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Do not have permission to enter this environment'
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (environment === 'dev' && pnid.access_level < 3) {
|
||||
response.status(400).json({
|
||||
app: 'api',
|
||||
status: 400,
|
||||
error: 'Do not have permission to enter this environment'
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
updateData.server_access_level = environment;
|
||||
}
|
||||
|
||||
await PNID.updateOne({ pid: pnid.pid }, { $set: updateData }).exec();
|
||||
|
||||
response.json({
|
||||
access_level: pnid.access_level,
|
||||
|
||||
1
src/types/mii-js.d.ts
vendored
1
src/types/mii-js.d.ts
vendored
@@ -96,4 +96,5 @@ type Mii = {
|
||||
|
||||
studioUrl: (options: MiiStudioURLOptions) => string;
|
||||
encode: () => Buffer;
|
||||
validate: () => void;
|
||||
};
|
||||
Reference in New Issue
Block a user