Skip to content

Commit

Permalink
Schéma de équipement
Browse files Browse the repository at this point in the history
  • Loading branch information
SonofaPancak committed Sep 15, 2023
1 parent 808c534 commit bc660c1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/Entities/EntityTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export enum EntityTypesEnum {
taxonomy = "Taxonomy",
event = "Event",
media = "Media",
place = "Place"
place = "Place",
equipement = "Equipement"
}

export enum EntityApiUrlEnum {
Expand All @@ -16,5 +17,6 @@ export enum EntityApiUrlEnum {
taxonomy = "taxonomies",
event = "events",
media = "medias",
place = "places"
place = "places",
equipement = "equipements"
}
97 changes: 84 additions & 13 deletions src/Equipments/Models/Equipment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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';

Expand All @@ -42,7 +67,41 @@ class Equipment extends AbstractModel {

/** @public Database schema */
schema: Schema =
new Schema<EquipmentSchema>({},
new Schema<EquipmentSchema>({
//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,
Expand All @@ -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.
Expand All @@ -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 ?? '',
}
}

Expand All @@ -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);
});
}
}

Expand Down
24 changes: 22 additions & 2 deletions src/Equipments/Routes/EquipmentsRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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: [],
Expand Down
13 changes: 11 additions & 2 deletions src/Equipments/Schemas/EquipmentSchema.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 3 additions & 1 deletion src/Media/Controllers/MediasController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit bc660c1

Please sign in to comment.