Modify Asset Model to suit new hierach structure as above
Last active
July 9, 2020 10:42
-
-
Save paulobunga/4079854f378d2a0f7953c3ca08474de2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AssetSchema = new Schema({ | |
assetTag: { type: String, required: true }, | |
assetName: { type: String, required: true }, | |
assetDescription: { type: String, required: true }, | |
assetCatNo: { type: String, required: true }, | |
assetStatus: { type: String }, | |
storageLatitude: { type: String }, | |
storageLongitude: { type: String}, | |
assetSerialNumber: { type: String}, | |
assetType: { type: String }, | |
others: { type: Object }, | |
parent: { | |
type: Schema.Types.ObjectId, | |
default: null, | |
ref: "Asset", | |
}, | |
ancestors: [ | |
{ | |
_id: { | |
type: mongoose.Schema.Types.ObjectId, | |
ref: "Asset", | |
}, | |
name: String, | |
slug: String, | |
}, | |
], | |
}, { | |
timestamps: true | |
}); | |
AssetSchema.pre("save", async (next) => { | |
this.slug = createSlug(this.assetType); | |
next(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default buildAncestors = async (id, parent_id) => { | |
let ancesta = []; | |
try { | |
let parent_asset = await Asset.findOne( | |
{ _id: parent_id }, | |
{ name: 1, slug: 1, ancestors: 1 } | |
).exec(); | |
if (parent_asset) { | |
const { _id, name, slug } = parent_asset; | |
const ancesta = [...parent_asset.ancestors]; | |
ancesta.unshift({ _id, name, slug }); | |
const asset = await Asset.findByIdAndUpdate(id, { | |
$set: { ancestors: ancesta }, | |
}); | |
} | |
} catch (err) { | |
console.log(err.message); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.post("/", async (req, res) => { | |
let asset = new Asset(); | |
//ASSET | |
const assetTag = req.body.assetTag; | |
const assetName = req.body.assetName; | |
const assetDescription = req.body.assetDescription; | |
const assetCatNo = req.body.assetCatNo; | |
const assetStatus = "receive"; | |
const storageLatitude = req.body.assetLatitude; | |
const storageLongitude = req.body.assetLongitude; | |
const assetSerialNumber = req.body.assetSerialNumber; | |
const assetType = req.body.assetType; //PALLETE //CRATE OR UNIT | |
const asset = new Asset({ | |
assetTag, | |
assetName, | |
assetDescription, | |
assetCatNo, | |
assetStatus, | |
storageLatitude, | |
storageLongitude, | |
assetSerialNumber, | |
others, | |
assetType, | |
parent: req.body.assetType ? req.body.assetType : null | |
}); | |
asset.save((err, result) => { | |
if(result) { | |
buildAncestors(asset._id, req.body.assetType) | |
} | |
}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default createSlug = function slugify(string) { | |
const a = "àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;"; | |
const b = "aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------"; | |
const p = new RegExp(a.split("").join("|"), "g"); | |
return string | |
.toString() | |
.toLowerCase() | |
.replace(/\s+/g, "-") | |
.replace(p, (c) => b.charAt(a.indexOf(c))) | |
.replace(/&/g, "-and-") | |
.replace(/[^\w\-]+/g, "") | |
.replace(/\-\-+/g, "-") | |
.replace(/^-+/, "") | |
.replace(/-+$/, ""); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment