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 DatabaseService from "./DatabaseService";
import Model from "../../Models/DatabaseModels/WorkspaceProjectAuthToken";
import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
import BadDataException from "../../Types/Exception/BadDataException";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async getProjectAuth(data) {
        if (!data.projectId) {
            throw new BadDataException("projectId is required");
        }
        if (!data.workspaceType) {
            throw new BadDataException("workspaceType is required");
        }
        return await this.findOneBy({
            query: {
                projectId: data.projectId,
                workspaceType: data.workspaceType,
            },
            select: {
                authToken: true,
                workspaceProjectId: true,
                miscData: true,
                workspaceType: true,
            },
            props: {
                isRoot: true,
            },
        });
    }
    async getProjectAuths(data) {
        if (!data.projectId) {
            throw new BadDataException("projectId is required");
        }
        return await this.findBy({
            query: {
                projectId: data.projectId,
            },
            select: {
                authToken: true,
                workspaceProjectId: true,
                miscData: true,
                workspaceType: true,
            },
            skip: 0,
            limit: LIMIT_PER_PROJECT,
            props: {
                isRoot: true,
            },
        });
    }
    async doesExist(data) {
        return Boolean(await this.getProjectAuth(data));
    }
    async refreshAuthToken(data) {
        if (!data.projectId) {
            throw new BadDataException("projectId is required");
        }
        if (!data.workspaceType) {
            throw new BadDataException("workspaceType is required");
        }
        if (!data.authToken) {
            throw new BadDataException("authToken is required");
        }
        if (!data.workspaceProjectId) {
            throw new BadDataException("workspaceProjectId is required");
        }
        if (!data.miscData) {
            throw new BadDataException("miscData is required");
        }
        let projectAuth = await this.findOneBy({
            query: {
                projectId: data.projectId,
                workspaceType: data.workspaceType,
            },
            select: {
                _id: true,
            },
            props: {
                isRoot: true,
            },
        });
        if (!projectAuth) {
            projectAuth = new Model();
            projectAuth.projectId = data.projectId;
            projectAuth.authToken = data.authToken;
            projectAuth.workspaceType = data.workspaceType;
            projectAuth.workspaceProjectId = data.workspaceProjectId;
            projectAuth.miscData = data.miscData;
            await this.create({
                data: projectAuth,
                props: {
                    isRoot: true,
                },
            });
        }
        else {
            await this.updateOneById({
                id: projectAuth.id,
                data: {
                    authToken: data.authToken,
                    workspaceProjectId: data.workspaceProjectId,
                    miscData: data.miscData,
                },
                props: {
                    isRoot: true,
                },
            });
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "getProjectAuth", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "getProjectAuths", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "doesExist", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "refreshAuthToken", null);
export default new Service();
//# sourceMappingURL=WorkspaceProjectAuthTokenService.js.map