From bc660c1bb551e3598586f9e1d2d0c995f1f6e553 Mon Sep 17 00:00:00 2001 From: SonofaPancak Date: Fri, 15 Sep 2023 13:44:59 -0400 Subject: [PATCH] =?UTF-8?q?Sch=C3=A9ma=20de=20=C3=A9quipement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Entities/EntityTypes.ts | 6 +- src/Equipments/Models/Equipment.ts | 97 ++++++++++++++++++++--- src/Equipments/Routes/EquipmentsRoute.ts | 24 +++++- src/Equipments/Schemas/EquipmentSchema.ts | 13 ++- src/Media/Controllers/MediasController.ts | 4 +- 5 files changed, 124 insertions(+), 20 deletions(-) diff --git a/src/Entities/EntityTypes.ts b/src/Entities/EntityTypes.ts index 1f3949c5..4dbcfbe0 100644 --- a/src/Entities/EntityTypes.ts +++ b/src/Entities/EntityTypes.ts @@ -6,7 +6,8 @@ export enum EntityTypesEnum { taxonomy = "Taxonomy", event = "Event", media = "Media", - place = "Place" + place = "Place", + equipement = "Equipement" } export enum EntityApiUrlEnum { @@ -16,5 +17,6 @@ export enum EntityApiUrlEnum { taxonomy = "taxonomies", event = "events", media = "medias", - place = "places" + place = "places", + equipement = "equipements" } \ No newline at end of file diff --git a/src/Equipments/Models/Equipment.ts b/src/Equipments/Models/Equipment.ts index 005ed2f7..3f5ffb62 100644 --- a/src/Equipments/Models/Equipment.ts +++ b/src/Equipments/Models/Equipment.ts @@ -3,6 +3,10 @@ import AbstractModel from "@core/Model"; import type {DbProvider} from "@database/DatabaseDomain"; import {EquipmentSchema} from "@src/Equipments/Schemas/EquipmentSchema"; import EquipmentsService from "@src/Equipments/Services/EquipmentsService"; +import { Meta } from "@src/Moderation/Schemas/MetaSchema"; +import { populateUser } from "@src/Users/Middlewares/populateUser"; +import { middlewarePopulateProperty } from "@src/Taxonomy/Middlewares/TaxonomiesPopulate"; +import { User } from "@src/Users/UsersDomain"; class Equipment extends AbstractModel { @@ -18,16 +22,37 @@ class Equipment extends AbstractModel { Equipment._instance.registerEvents(); //Setting virtuals - //Equipment._instance.schema.virtual("Equipment").get( function () { return Equipment._instance.modelName }); + Equipment._instance.schema.virtual("type").get( function () { return Equipment._instance.modelName }); + Equipment._instance.schema.virtual("name").get( function () { return this.brand + ' ' + this.model + ' ' + this.label }); Equipment._instance.initSchema(); //Index - //Equipment._instance.schema.index({ "Equipment.Equipment":1}); + Equipment._instance.registerIndexes(); } return Equipment._instance; } + public registerIndexes(): void { + //Indexes + Equipment._instance.schema.index( + { brand:"text", model:"text", label:"text", slug:"text" }, + { + default_language: "french", + //Note: if changed, make sure database really changed it by usings compass or mongosh (upon restart doesn't seem like it) + weights:{ + brand:5, + model:5, + label:3 + } + }); + return; + } + + public dropIndexes(): void { + return; + } + /** @public Model lastName */ modelName: string = 'Equipment'; @@ -42,7 +67,41 @@ class Equipment extends AbstractModel { /** @public Database schema */ schema: Schema = - new Schema({}, + new Schema({ + //name (virtual) + equipementType: { + type: String + }, + label: { + type: String + }, + description: { + type: String + }, + brand: { + type: String + }, + model: { + type: String + }, + slug: { + type: String, + slug: ["brand", "model", "label"], + slugPaddingSize: 3, + index: true, + unique: true + }, + mainImage: { + type: mongoose.Types.ObjectId, + ref : "Media" + }, + url: { + type: String + }, + meta: { + type: Meta.schema + } + }, { toJSON: {virtuals: true}, timestamps: true, @@ -54,15 +113,6 @@ class Equipment extends AbstractModel { /** @abstract Set of rules that are verified for every field of this entity. */ public ruleSet: any = []; - - public registerIndexes(): void { - return; - }; - - public dropIndexes(): void { - return; - }; - /** * @get the field that are searchable. * @return {Object} the field slug/names. @@ -80,6 +130,18 @@ class Equipment extends AbstractModel { public dataTransfertObject(document: any) { return { _id: document._id ?? '', + name: document.name ?? '', + label: document.label ?? '', + description: document.description ?? '', + brand: document.brand ?? '', + model: document.model ?? '', + slug: document.slug ?? '', + mainImage: document.mainImage ?? '', + url: document.url ?? '', + equipementType: document.equipementType ?? '', + meta: document.meta ?? {}, + createAt: document.createAt ?? '', + updatedAt: document.updatedAt ?? '', } } @@ -92,7 +154,16 @@ class Equipment extends AbstractModel { * Register mongoose events, for now pre-save, pre-findOneAndUpdate */ public registerEvents(): void { - + this.schema.pre('find', function() { + middlewarePopulateProperty(this, "mainImage"); + populateUser(this, "meta.requestedBy", User.getInstance().mongooseModel); + populateUser(this, "meta.lastModifiedBy", User.getInstance().mongooseModel); + }); + this.schema.pre('findOne', function() { + middlewarePopulateProperty(this, 'mainImage'); + populateUser(this, "meta.requestedBy", User.getInstance().mongooseModel); + populateUser(this, "meta.lastModifiedBy", User.getInstance().mongooseModel); + }); } } diff --git a/src/Equipments/Routes/EquipmentsRoute.ts b/src/Equipments/Routes/EquipmentsRoute.ts index ac677957..2c65c0b2 100644 --- a/src/Equipments/Routes/EquipmentsRoute.ts +++ b/src/Equipments/Routes/EquipmentsRoute.ts @@ -2,6 +2,9 @@ import express from "express"; import AbstractController from "@core/Controller"; import CrudRoute from "@core/CrudRoute"; import EquipmentsController from "@src/Equipments/Controllers/EquipmentsController"; +import { basicHtmlSanitizerAlias } from "@src/Security/SanitizerAliases/BasicHtmlSanitizerAlias"; +import { objectIdSanitizerAlias } from "@src/Security/SanitizerAliases/ObjectIdSanitizerAlias"; +import { noHtmlStringSanitizerAlias } from "@src/Security/SanitizerAliases/NoHtmlStringSanitizerAlias"; class EquipmentsRoutes extends CrudRoute { @@ -12,8 +15,25 @@ class EquipmentsRoutes extends CrudRoute { middlewaresDistribution: any = { all: [], createUpdate: [], - create: [], - update: [], + create: [ + noHtmlStringSanitizerAlias('data.equipementType'), + noHtmlStringSanitizerAlias('data.label'), + basicHtmlSanitizerAlias('data.description'), + noHtmlStringSanitizerAlias('data.brand'), + noHtmlStringSanitizerAlias('data.model'), + objectIdSanitizerAlias('data.mainImage'), + noHtmlStringSanitizerAlias('data.url') + ], + update: [ + objectIdSanitizerAlias('data.id', false), + noHtmlStringSanitizerAlias('data.equipementType'), + noHtmlStringSanitizerAlias('data.label'), + basicHtmlSanitizerAlias('data.description'), + noHtmlStringSanitizerAlias('data.brand'), + noHtmlStringSanitizerAlias('data.model'), + objectIdSanitizerAlias('data.mainImage'), + noHtmlStringSanitizerAlias('data.url') + ], delete: [], search: [], list: [], diff --git a/src/Equipments/Schemas/EquipmentSchema.ts b/src/Equipments/Schemas/EquipmentSchema.ts index a65e52e3..564bdde0 100644 --- a/src/Equipments/Schemas/EquipmentSchema.ts +++ b/src/Equipments/Schemas/EquipmentSchema.ts @@ -1,5 +1,14 @@ -import {Document} from "mongoose"; +import { Meta } from "@src/Moderation/Schemas/MetaSchema"; +import {Document, ObjectId} from "mongoose"; export interface EquipmentSchema extends Document { - + equipementType:string; + label:string; + description:string; + brand:string; + model:string; + slug:string; + mainImage:ObjectId; + url:string; + meta:Meta; } \ No newline at end of file diff --git a/src/Media/Controllers/MediasController.ts b/src/Media/Controllers/MediasController.ts index 60f4a600..eb21bbe8 100644 --- a/src/Media/Controllers/MediasController.ts +++ b/src/Media/Controllers/MediasController.ts @@ -11,6 +11,7 @@ import Project from "@src/Projects/Models/Project"; import Place from "@src/Places/Models/Place"; import Event from "@src/Events/Models/Event"; import EntityControllerFactory from "@src/Abstract/EntityControllerFactory"; +import Equipment from "@src/Equipments/Models/Equipment"; class MediasController extends AbstractController { //implements ControllerContract { @@ -88,7 +89,8 @@ class MediasController extends AbstractController { //implements ControllerContr [Organisation.getInstance().modelName]: ["mainImage"], [Project.getInstance().modelName]: ["mainImage"], [Event.getInstance().modelName]: ["mainImage", "photoGallery"], - [Place.getInstance().modelName]: ["mainImage"] + [Place.getInstance().modelName]: ["mainImage"], + [Equipment.getInstance().modelName]: ["mainImage"] }; //For all keys (modelName) check if image is accepted if(!Object.keys(entities).includes(requestData.entityType))