feat: import #831
@ -4,7 +4,7 @@ import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
|
||||
import { config } from "@/src/services/configService";
|
||||
import allDialogue from "@/static/fixed_responses/allDialogue.json";
|
||||
import { ILoadoutDatabase } from "@/src/types/saveLoadoutTypes";
|
||||
import { IInventoryResponse, IShipInventory, equipmentKeys } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { IInventoryClient, IShipInventory, equipmentKeys } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { IPolarity, ArtifactPolarity, EquipmentFeatures } from "@/src/types/inventoryTypes/commonInventoryTypes";
|
||||
import {
|
||||
ExportCustoms,
|
||||
@ -55,7 +55,7 @@ export const inventoryController: RequestHandler = async (request, response) =>
|
||||
const inventoryWithLoadOutPresetsAndShips = await inventoryWithLoadOutPresets.populate<{ Ships: IShipInventory }>(
|
||||
"Ships"
|
||||
);
|
||||
const inventoryResponse = inventoryWithLoadOutPresetsAndShips.toJSON<IInventoryResponse>();
|
||||
const inventoryResponse = inventoryWithLoadOutPresetsAndShips.toJSON<IInventoryClient>();
|
||||
|
||||
if (config.infiniteCredits) {
|
||||
inventoryResponse.RegularCredits = 999999999;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { importInventory } from "@/src/services/importService";
|
||||
import { getInventory } from "@/src/services/inventoryService";
|
||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||
import { IInventoryResponse } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { IInventoryClient } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
export const importController: RequestHandler = async (req, res) => {
|
||||
@ -13,5 +13,5 @@ export const importController: RequestHandler = async (req, res) => {
|
||||
};
|
||||
|
||||
|
||||
interface IImportRequest {
|
||||
inventory: IInventoryResponse;
|
||||
inventory: IInventoryClient;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import {
|
||||
IMiscItem,
|
||||
IInventoryDatabase,
|
||||
IBooster,
|
||||
IInventoryResponse,
|
||||
IInventoryClient,
|
||||
ISlots,
|
||||
IMailbox,
|
||||
IDuviriInfo,
|
||||
@ -1142,7 +1142,7 @@ inventorySchema.set("toJSON", {
|
||||
delete returnedObject.accountOwnerId;
|
||||
|
||||
const inventoryDatabase = returnedObject as IInventoryDatabase;
|
||||
const inventoryResponse = returnedObject as IInventoryResponse;
|
||||
const inventoryResponse = returnedObject as IInventoryClient;
|
||||
|
||||
inventoryResponse.TrainingDate = toMongoDate(inventoryDatabase.TrainingDate);
|
||||
inventoryResponse.Created = toMongoDate(inventoryDatabase.Created);
|
||||
|
@ -3,7 +3,7 @@
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
import { Types } from "mongoose";
|
||||
import { IEquipmentClient, IEquipmentDatabase } from "../types/inventoryTypes/commonInventoryTypes";
|
||||
import { IMongoDate } from "../types/commonTypes";
|
||||
import { IInventoryResponse, IWeaponSkinClient, IWeaponSkinDatabase } from "../types/inventoryTypes/inventoryTypes";
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
import { IInventoryClient, IWeaponSkinClient, IWeaponSkinDatabase } from "../types/inventoryTypes/inventoryTypes";
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
import { TInventoryDatabaseDocument } from "../models/inventoryModels/inventoryModel";
|
||||
|
||||
const convertDate = (value: IMongoDate): Date => {
|
||||
@ -40,7 +40,7 @@ const replaceArray = <T>(arr: T[], replacement: T[]): void => {
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
});
|
||||
};
|
||||
|
||||
export const importInventory = (db: TInventoryDatabaseDocument, client: IInventoryResponse): void => {
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
export const importInventory = (db: TInventoryDatabaseDocument, client: IInventoryClient): void => {
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
replaceArray<IEquipmentDatabase>(db.Suits, client.Suits.map(convertEquipment));
|
||||
replaceArray<IWeaponSkinDatabase>(db.WeaponSkins, client.WeaponSkins.map(convertWeaponSkin));
|
||||
![]() ⚠️ Potential issue Add validation for In the Apply this diff to add validation:
_:warning: Potential issue_
**Add validation for `ItemId.$oid` before creating `ObjectId` in `convertEquipment`**
In the `convertEquipment` function, if `ItemId` or `ItemId.$oid` is undefined or not a valid ObjectId, creating a new `ObjectId` could result in a runtime exception. It's important to add validation to ensure `ItemId.$oid` exists and is valid before proceeding.
Apply this diff to add validation:
```diff
const convertEquipment = (client: IEquipmentClient): IEquipmentDatabase => {
const { ItemId, ...rest } = client;
+ if (!ItemId || !ItemId.$oid || !Types.ObjectId.isValid(ItemId.$oid)) {
+ throw new Error('Invalid ItemId');
+ }
return {
...rest,
_id: new Types.ObjectId(ItemId.$oid),
InfestationDate: convertOptionalDate(client.InfestationDate),
Expiry: convertOptionalDate(client.Expiry),
UpgradesExpiry: convertOptionalDate(client.UpgradesExpiry)
};
};
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
};
|
||||
|
||||
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
![]() 🛠️ Refactor suggestion Consider using transactions for atomic operations. The Additionally, the function should validate the client object structure before processing.
_:hammer_and_wrench: Refactor suggestion_
**Consider using transactions for atomic operations.**
The `importInventory` function performs multiple updates that should be atomic. Consider wrapping the operations in a transaction.
Additionally, the function should validate the client object structure before processing.
```diff
export const importInventory = (db: TInventoryDatabaseDocument, client: Partial<IInventoryClient>): void => {
+ if (!db || !client) {
+ throw new Error('Invalid arguments: db and client are required');
+ }
+
+ // Validate required client properties
+ const requiredKeys = ['WeaponSkins', 'Upgrades'];
+ for (const key of requiredKeys) {
+ if (client[key] && !Array.isArray(client[key])) {
+ throw new Error(`Invalid ${key} format: expected array`);
+ }
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
@ -13,7 +13,7 @@ import {
|
||||
|
||||
export interface IInventoryDatabase
|
||||
extends Omit<
|
||||
IInventoryResponse,
|
||||
IInventoryClient,
|
||||
| "TrainingDate"
|
||||
| "LoadOutPresets"
|
||||
| "Mailbox"
|
||||
@ -129,7 +129,7 @@ export interface IDailyAffiliations {
|
||||
DailyAffiliationHex: number;
|
||||
}
|
||||
|
||||
export interface IInventoryResponse extends IDailyAffiliations {
|
||||
export interface IInventoryClient extends IDailyAffiliations {
|
||||
Horses: IEquipmentDatabase[];
|
||||
DrifterMelee: IEquipmentDatabase[];
|
||||
DrifterGuns: IEquipmentDatabase[];
|
||||
|
🛠️ Refactor suggestion
Implement error handling during import
Operations like
importInventory
andinventory.save()
can fail. Without error handling, any failures won't be communicated back to the client, and the server might crash due to unhandled exceptions.Wrap the import operations in a try-catch block and handle errors appropriately: