@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.
139 lines (138 loc) • 5.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "FloorService", {
enumerable: true,
get: function() {
return FloorService;
}
});
const _Floor = require("../../models/Floor");
const _validators = require("../../handlers/validators");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
const _floorservicevalidation = require("../validators/floor-service.validation");
class FloorService {
printerCache;
constructor(printerCache){
this.printerCache = printerCache;
}
toDto(floor) {
return {
id: floor.id,
name: floor.name,
floor: floor.floor,
printers: floor.printers?.map((p)=>({
x: p.x,
y: p.y,
printerId: p.printerId,
floorId: floor.id
}))
};
}
async list() {
return _Floor.Floor.find({});
}
async get(floorId) {
const floor = await _Floor.Floor.findOne({
_id: floorId
});
if (!floor) {
throw new _runtimeexceptions.NotFoundException(`Floor with provided id does not exist`);
}
return floor;
}
async createDefaultFloor() {
return await this.create({
name: "Default Floor",
floor: 1,
printers: []
});
}
async create(floor) {
const validatedInput = await (0, _validators.validateInput)(floor, (0, _floorservicevalidation.createOrUpdateFloorSchema)(false));
return _Floor.Floor.create(validatedInput);
}
async update(floorId, update) {
const existingFloor = await this.get(floorId);
if (update.printers) {
for (const position of update.printers){
position.floorId = existingFloor.id;
}
}
const floorUpdate = {
...existingFloor,
name: update.name,
printers: update.printers,
floor: update.floor
};
const input = await (0, _validators.validateInput)(floorUpdate, (0, _floorservicevalidation.createOrUpdateFloorSchema)(false));
existingFloor.name = input.name;
existingFloor.floor = input.floor;
existingFloor.printers = input.printers;
return await existingFloor.save();
}
async updateName(floorId, floorName) {
const { name } = await (0, _validators.validateInput)({
name: floorName
}, _floorservicevalidation.updateFloorNameSchema);
const floor = await this.get(floorId);
floor.name = name;
return await floor.save();
}
async updateLevel(floorId, level) {
const { floor: validLevel } = await (0, _validators.validateInput)({
floor: level
}, _floorservicevalidation.updateFloorLevelSchema);
const floor = await this.get(floorId);
floor.floor = validLevel;
return await floor.save();
}
async deletePrinterFromAnyFloor(printerId) {
await _Floor.Floor.updateMany({}, {
$pull: {
printers: {
printerId: {
$in: [
printerId
]
}
}
}
});
}
async addOrUpdatePrinter(floorId, updatedPosition) {
const floor = await this.get(floorId);
updatedPosition.floorId = floor.id;
const validInput = await (0, _validators.validateInput)(updatedPosition, (0, _floorservicevalidation.printerInFloorSchema)(false));
await this.printerCache.getCachedPrinterOrThrowAsync(validInput.printerId);
floor.printers = floor.printers.filter((position)=>!(position.x === validInput.x && position.y === validInput.y));
const positionIndex = floor.printers.findIndex((position)=>position.printerId.toString() === validInput.printerId.toString());
if (positionIndex !== -1) {
floor.printers[positionIndex] = validInput;
} else {
floor.printers.push(validInput);
}
await floor.save();
return floor;
}
async removePrinter(floorId, printerId) {
const floor = await this.get(floorId);
const validInput = await (0, _validators.validateInput)({
printerId
}, (0, _floorservicevalidation.removePrinterInFloorSchema)(false));
await this.printerCache.getCachedPrinterOrThrowAsync(validInput.printerId);
const foundPrinterInFloorIndex = floor.printers.findIndex((pif)=>pif.printerId.toString() === validInput.printerId.toString());
if (foundPrinterInFloorIndex === -1) return floor;
floor.printers.splice(foundPrinterInFloorIndex, 1);
await floor.save();
return floor;
}
async delete(floorId) {
await this.get(floorId);
await _Floor.Floor.deleteOne({
_id: floorId
});
}
}
//# sourceMappingURL=floor.service.js.map