var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import BadDataException from "../../Types/Exception/BadDataException";
import DatabaseService from "./DatabaseService";
import Model from "../../Models/DatabaseModels/ServiceCodeRepository";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async onBeforeCreate(createBy) {
        var _a, _b;
        if (!createBy.data.serviceId && !createBy.data.service) {
            throw new BadDataException("service is required");
        }
        if (!createBy.data.codeRepository && !createBy.data.codeRepositoryId) {
            throw new BadDataException("codeRepository is required");
        }
        const serviceId = createBy.data.serviceId || ((_a = createBy.data.service) === null || _a === void 0 ? void 0 : _a._id);
        const codeRepositoryId = createBy.data.codeRepositoryId || ((_b = createBy.data.codeRepository) === null || _b === void 0 ? void 0 : _b._id);
        // check if this code repository is already added to the service for this service.
        const existingCodeRepository = await this.findOneBy({
            query: {
                serviceId: serviceId,
                codeRepositoryId: codeRepositoryId,
            },
            props: {
                isRoot: true,
            },
        });
        if (existingCodeRepository) {
            throw new BadDataException("Code Repository already exists for this service");
        }
        return {
            carryForward: null,
            createBy: createBy,
        };
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeCreate", null);
export default new Service();
//# sourceMappingURL=ServiceCodeRepositoryService.js.map