@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
112 lines (111 loc) • 4.64 kB
JavaScript
import { NotFoundException } from "../../exceptions/runtime.exceptions.js";
import { BaseService } from "./base.service.js";
import { validateInput } from "../../handlers/validators.js";
import { createOrUpdateFloorSchema, printerInFloorSchema, updateFloorNameSchema, updateFloorOrderSchema } from "../validators/floor-service.validation.js";
import { FloorPosition } from "../../entities/floor-position.entity.js";
import { Floor } from "../../entities/floor.entity.js";
import "../../entities/index.js";
import { CreateFloorDto, FloorDto, UpdateFloorDto } from "../interfaces/floor.dto.js";
//#region src/services/orm/floor.service.ts
var FloorService = class extends BaseService(Floor, FloorDto, CreateFloorDto, UpdateFloorDto) {
constructor(typeormService, floorPositionService) {
super(typeormService);
this.typeormService = typeormService;
this.floorPositionService = floorPositionService;
}
async list(options) {
return super.list(Object.assign(options || {}, { relations: ["printers"] }));
}
async get(id, options) {
return super.get(id, Object.assign(options || {}, { relations: ["printers"] }));
}
async create(dto) {
const outcome = await validateInput(dto, createOrUpdateFloorSchema);
const floor = await super.create({
name: outcome.name,
order: outcome.order,
printers: []
});
if (outcome.printers?.length) for (const position of outcome.printers) await this.addOrUpdatePrinter(floor.id, position);
return this.get(floor.id);
}
toDto(floor) {
return {
id: floor.id,
name: floor.name,
order: floor.order,
printers: floor.printers.map((p) => ({
printerId: p.printerId,
floorId: p.floorId,
x: p.x,
y: p.y
}))
};
}
/**
* This is an overwriting method. Any missing data will be deleted, and can cause sql errors if causing wrong constraints.
* Merge data before calling this function.
* @param floorId
* @param update
*/
async update(floorId, update) {
const existingFloor = await this.get(floorId);
const floorUpdate = {
...existingFloor,
name: update.name,
printers: update.printers,
order: update.order
};
const desiredPositions = (await validateInput(floorUpdate, createOrUpdateFloorSchema)).printers;
if (desiredPositions?.length) {
for (const printer of desiredPositions) await this.addOrUpdatePrinter(existingFloor.id, printer);
const undesiredPositions = existingFloor.printers.filter((pos) => !desiredPositions.find((dp) => dp.printerId === pos.printerId));
if (undesiredPositions?.length) await this.floorPositionService.deleteMany(undesiredPositions.map((pos) => pos.id));
}
delete floorUpdate.printers;
return super.update(floorId, floorUpdate);
}
async updateName(floorId, floorName) {
const { name } = await validateInput({ name: floorName }, updateFloorNameSchema);
const floor = await this.get(floorId);
floor.name = name;
return this.update(floorId, floor);
}
async updateOrder(floorId, order) {
const { order: validOrder } = await validateInput({ order }, updateFloorOrderSchema);
const floor = await this.get(floorId);
floor.order = validOrder;
return await this.update(floorId, floor);
}
async addOrUpdatePrinter(floorId, positionDto) {
await this.get(floorId);
positionDto.floorId = floorId;
const validInput = await validateInput(positionDto, printerInFloorSchema);
const position = await this.floorPositionService.findPrinterPosition(validInput.printerId);
if (position?.floorId === floorId && position.x === validInput.x && position.y === validInput.y && position.printerId === validInput.printerId) return this.get(floorId);
if (position) await this.floorPositionService.delete(position.id);
const xyPosition = await this.floorPositionService.findPosition(floorId, validInput.x, validInput.y);
if (xyPosition) await this.floorPositionService.delete(xyPosition.id);
const newPosition = new FloorPosition();
Object.assign(newPosition, {
x: validInput.x,
y: validInput.y,
printerId: validInput.printerId,
floorId
});
await this.floorPositionService.create(newPosition);
return this.get(floorId);
}
async removePrinter(floorId, printerId) {
const position = await this.floorPositionService.findPrinterPositionOnFloor(floorId, printerId);
if (!position) throw new NotFoundException("This printer was not found on this floor");
await this.floorPositionService.delete(position.id);
return await this.get(floorId);
}
async deletePrinterFromAnyFloor(printerId) {
await this.floorPositionService.deletePrinterPositionsByPrinterId(printerId);
}
};
//#endregion
export { FloorService };
//# sourceMappingURL=floor.service.js.map