Skip to content

Commit

Permalink
refactored item validations and changed items routes privilages
Browse files Browse the repository at this point in the history
  • Loading branch information
airfortech committed Mar 29, 2024
1 parent a799fad commit 24e96c5
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 72 deletions.
91 changes: 49 additions & 42 deletions controllers/items/getItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const getItems = async (req: Request, res: Response) => {
typeof req.query.isMagic === "string"
? JSON.parse(req.query.isMagic)
: undefined;
const isWeaponSilver =
typeof req.query.isWeaponSilver === "string"
? JSON.parse(req.query.isWeaponSilver)
: undefined;
const type = ItemTypes[req.query.type as keyof typeof ItemTypes];
const weaponType =
ItemWeapon[req.query.weaponType as keyof typeof ItemWeapon];
Expand All @@ -21,51 +25,17 @@ export const getItems = async (req: Request, res: Response) => {

const items = await Item.find({
...(isMagic !== undefined && { isMagic }),
...(isWeaponSilver !== undefined && { isWeaponSilver }),
type,
...(weaponType !== undefined && { weaponType }),
...(armorClass !== undefined && { armorClass }),
});
return res.status(200).json({
status: Status.success,
data: {
items: items.map(
({
id,
name,
short,
isMagic,
type,
weaponType,
weaponHand,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
isWeaponSilver,
armorClass,
armorHead,
armorLeftArm,
armorRightArm,
armorChest,
armorLegs,
armorFoots,
armorHands,
armorPiercingRes,
armorSlashingRes,
armorBluntRes,
shieldParry,
weight,
volume,
durability,
specialBonus,
occurrence,
cost,
vendorCost,
description,
comment,
}) => {
const data: ItemResponse = {
items: items
.map(
({
id,
name,
short,
Expand Down Expand Up @@ -100,10 +70,47 @@ export const getItems = async (req: Request, res: Response) => {
vendorCost,
description,
comment,
};
return data;
}
),
}) => {
const data: ItemResponse = {
id,
name,
short,
isMagic,
type,
weaponType,
weaponHand,
weaponSlashingDamage,
weaponPiercingDamage,
weaponBluntDamage,
weaponEffectiveness,
weaponBalance,
isWeaponSilver,
armorClass,
armorHead,
armorLeftArm,
armorRightArm,
armorChest,
armorLegs,
armorFoots,
armorHands,
armorPiercingRes,
armorSlashingRes,
armorBluntRes,
shieldParry,
weight,
volume,
durability,
specialBonus,
occurrence,
cost,
vendorCost,
description,
comment,
};
return data;
}
)
.sort((a, b) => a.short.localeCompare(b.short)),
},
});
} catch (e) {
Expand Down
16 changes: 9 additions & 7 deletions controllers/items/updateItem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { messages, Status } from "../../types/responseMessages";
import { KeyUpdateRequest } from "../../types/Key";
import { Request } from "../../types/Request";
import { ItemTypes } from "../../types/ItemTypes";
import { Item } from "../../db/models/Item";
Expand All @@ -16,7 +15,7 @@ export const updateItem = async (req: Request, res: Response) => {
try {
const _id = req.params.id;
isIdValid(_id, messages[req.lang].items.itemNotExists, 404);
const data = req.body as KeyUpdateRequest;
const data = req.body as ItemUpdateRequest;
const type = req.body.type as ItemTypes;
if (!type)
throw new CustomError(messages[req.lang].items.noTypeProvided, 400);
Expand All @@ -28,18 +27,21 @@ export const updateItem = async (req: Request, res: Response) => {
: type === ItemTypes.shield
? updateShield(data as ItemUpdateRequest, type)
: updateOtherItem(data as ItemUpdateRequest, type);
const item = await Item.findOneAndUpdate(
{ _id },
{ ...args },
{ runValidators: true }
);
const item = await Item.findOne({
_id,
});
if (!item)
throw new CustomError(
messages[req.lang].items.itemNotExists,
404,
Status.error
);
const short = req.body.short?.trim().toLowerCase() || item.short;
await Item.findOneAndUpdate(
{ _id },
{ ...args, short: short === item.short ? undefined : short },
{ runValidators: true }
);
return res.status(200).json({
status: Status.success,
message: messages[req.lang].items.itemUpdated(short),
Expand Down
64 changes: 59 additions & 5 deletions db/models/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ const itemSchema = new Schema<IItem>(
unique: true,
validate: {
validator: async (short: string) => {
const existingKeyGiver = await mongoose
.model("Item")
.findOne({ short });
return !existingKeyGiver;
const existingItem = await mongoose.model("Item").findOne({ short });
return !existingItem;
},
message: ({ value }) =>
messages[config.lang].items.itemShortExists(value),
Expand Down Expand Up @@ -81,12 +79,24 @@ const itemSchema = new Schema<IItem>(
min: [1, messages[config.lang].items.weaponEffectivenessTooLow],
max: [14, messages[config.lang].items.weaponEffectivenessTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.weaponEffectivenessNotInteger,
},
},
weaponBalance: {
type: Number,
min: [1, messages[config.lang].items.weaponBalanceTooLow],
max: [14, messages[config.lang].items.weaponBalanceTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.weaponBalanceNotInteger,
},
},
isWeaponSilver: {
type: Boolean,
Expand Down Expand Up @@ -133,32 +143,72 @@ const itemSchema = new Schema<IItem>(
min: [1, messages[config.lang].items.armorResTooLow],
max: [12, messages[config.lang].items.armorResTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.armorResNotInteger,
},
},
armorSlashingRes: {
type: Number,
min: [1, messages[config.lang].items.armorResTooLow],
max: [12, messages[config.lang].items.armorResTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.armorResNotInteger,
},
},
armorBluntRes: {
type: Number,
min: [1, messages[config.lang].items.armorResTooLow],
max: [12, messages[config.lang].items.armorResTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.armorResNotInteger,
},
},
shieldParry: {
type: Number,
min: [1, messages[config.lang].items.shieldParryTooLow],
max: [12, messages[config.lang].items.shieldParryTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.shieldParryNotInteger,
},
},
weight: {
type: Number,
min: [1, messages[config.lang].items.itemWeightTooLow],
max: [1000000, messages[config.lang].items.itemWeightTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.itemWeightNotInteger,
},
},
volume: {
type: Number,
min: [1, messages[config.lang].items.itemVolumeTooLow],
max: [1000000, messages[config.lang].items.itemVolumeTooHigh],
default: null,
validate: {
validator: (value: number | null) => {
return value === null || Number.isInteger(value);
},
message: messages[config.lang].items.itemVolumeNotInteger,
},
},
durability: {
type: String,
Expand All @@ -172,7 +222,7 @@ const itemSchema = new Schema<IItem>(
type: String,
trim: true,
maxLength: [400, messages[config.lang].items.specialBonusTooLong],
default: null,
default: "",
},
occurrence: {
type: String,
Expand All @@ -182,10 +232,14 @@ const itemSchema = new Schema<IItem>(
},
cost: {
type: Number,
min: [0, messages[config.lang].items.itemCostTooLow],
max: [10000, messages[config.lang].items.itemCostTooHigh],
default: null,
},
vendorCost: {
type: Number,
min: [0, messages[config.lang].items.itemVendorCostTooLow],
max: [10000, messages[config.lang].items.itemVendorCostTooHigh],
default: null,
},
description: {
Expand Down
8 changes: 3 additions & 5 deletions db/tools/createDb/data/items/armors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ export const armors: Item[] = randomShorts(
short,
isMagic,
type: ItemTypes.armor,
armorClass: randomFromOptions(
randomFromArray(Object.values(ItemArmorClass)) as ItemArmorClass,
null,
config.tests.armorClassSpecifiedPercentage
),
armorClass: randomFromArray(
Object.values(ItemArmorClass)
) as ItemArmorClass,
armorHead: areBodyPartsProvided ? randomFromArray([true, false]) : null,
armorLeftArm: areBodyPartsProvided ? randomFromArray([true, false]) : null,
armorRightArm: areBodyPartsProvided ? randomFromArray([true, false]) : null,
Expand Down
Loading

0 comments on commit 24e96c5

Please sign in to comment.