remove keys from object helper func
This commit is contained in:
parent
e6708152c0
commit
4cff2ec886
@ -1,18 +1,15 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
||||||
import { RequestHandler } from "express";
|
|
||||||
|
|
||||||
import config from "@/config.json";
|
import config from "@/config.json";
|
||||||
|
import { removeKeysFromObject } from "@/src/helpers/general";
|
||||||
import { toLoginRequest } from "@/src/helpers/loginHelpers";
|
import { toLoginRequest } from "@/src/helpers/loginHelpers";
|
||||||
import { Account } from "@/src/models/loginModel";
|
import { Account } from "@/src/models/loginModel";
|
||||||
import { createAccount, isCorrectPassword } from "@/src/services/loginService";
|
import { createAccount, isCorrectPassword } from "@/src/services/loginService";
|
||||||
import { ILoginResponse } from "@/src/types/loginTypes";
|
import { ILoginResponse } from "@/src/types/loginTypes";
|
||||||
import { DTLS, groups, HUB, IRC, Nonce, NRS, platformCDNs } from "@/static/fixed_responses/login_static";
|
import { DTLS, HUB, IRC, NRS, Nonce, groups, platformCDNs } from "@/static/fixed_responses/login_static";
|
||||||
|
import { RequestHandler } from "express";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
const loginController: RequestHandler = async (request, response) => {
|
const loginController: RequestHandler = async (request, response) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
|
const body: unknown = JSON.parse(String(request.body));
|
||||||
const body = JSON.parse(request.body); // parse octet stream of json data to json object
|
|
||||||
const loginRequest = toLoginRequest(body);
|
const loginRequest = toLoginRequest(body);
|
||||||
|
|
||||||
const account = await Account.findOne({ email: loginRequest.email }); //{ _id: 0, __v: 0 }
|
const account = await Account.findOne({ email: loginRequest.email }); //{ _id: 0, __v: 0 }
|
||||||
@ -30,9 +27,8 @@ const loginController: RequestHandler = async (request, response) => {
|
|||||||
ConsentNeeded: false,
|
ConsentNeeded: false,
|
||||||
TrackedSettings: []
|
TrackedSettings: []
|
||||||
});
|
});
|
||||||
console.log("creating new account");
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
const databaseAccount = removeKeysFromObject(newAccount, ["email", "password"]);
|
||||||
const { email, password, ...databaseAccount } = newAccount;
|
|
||||||
const newLoginResponse: ILoginResponse = {
|
const newLoginResponse: ILoginResponse = {
|
||||||
...databaseAccount,
|
...databaseAccount,
|
||||||
Groups: groups,
|
Groups: groups,
|
||||||
@ -61,7 +57,7 @@ const loginController: RequestHandler = async (request, response) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { email, password, ...databaseAccount } = account.toJSON();
|
const databaseAccount = removeKeysFromObject(account.toJSON(), ["email", "password"]);
|
||||||
const newLoginResponse: ILoginResponse = {
|
const newLoginResponse: ILoginResponse = {
|
||||||
...databaseAccount,
|
...databaseAccount,
|
||||||
Groups: groups,
|
Groups: groups,
|
||||||
|
@ -55,4 +55,21 @@ const parseBoolean = (booleanCandidate: unknown): boolean => {
|
|||||||
return booleanCandidate;
|
return booleanCandidate;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { isString, isNumber, parseString, parseNumber, parseDateNumber, parseBoolean, parseEmail };
|
function removeKeysFromObject<T>(object: T, keys: Array<keyof T>): Partial<T> {
|
||||||
|
const newObject: Partial<T> = object;
|
||||||
|
for (const key of keys) {
|
||||||
|
delete newObject[key];
|
||||||
|
}
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
isString,
|
||||||
|
isNumber,
|
||||||
|
parseString,
|
||||||
|
parseNumber,
|
||||||
|
parseDateNumber,
|
||||||
|
parseBoolean,
|
||||||
|
parseEmail,
|
||||||
|
removeKeysFromObject
|
||||||
|
};
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import { IInventoryDatabase, IInventoryResponse } from "@/src/types/inventoryTypes";
|
import { removeKeysFromObject } from "@/src/helpers/general";
|
||||||
|
import { IInventoryDatabase } from "@/src/types/inventoryTypes";
|
||||||
|
|
||||||
const toInventoryResponse = (inventoryDatabase: IInventoryDatabase): IInventoryResponse => {
|
const toInventoryResponse = (inventoryDatabase: IInventoryDatabase) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
return removeKeysFromObject(inventoryDatabase, ["accountOwnerId"]);
|
||||||
const { accountOwnerId, ...inventoreResponse } = inventoryDatabase;
|
|
||||||
return inventoreResponse;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { toInventoryResponse };
|
export { toInventoryResponse };
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export interface ILoginResponse extends Omit<IDatabaseAccountDocument, "email" | "password"> {
|
export interface ILoginResponse extends Omit<Partial<IDatabaseAccountDocument>, "email" | "password"> {
|
||||||
Groups: IGroup[];
|
Groups: IGroup[];
|
||||||
Nonce: number;
|
Nonce: number;
|
||||||
BuildLabel: string;
|
BuildLabel: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user