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/WorkspaceUserAuthToken";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async getUserAuth(data) {
        return await this.findOneBy({
            query: {
                userId: data.userId,
                projectId: data.projectId,
                workspaceType: data.workspaceType,
            },
            select: {
                authToken: true,
                workspaceUserId: true,
                miscData: true,
                workspaceType: true,
            },
            props: {
                isRoot: true,
            },
        });
    }
    async doesExist(data) {
        return ((await this.countBy({
            query: {
                projectId: data.projectId,
                userId: data.userId,
                workspaceType: data.workspaceType,
            },
            skip: 0,
            limit: 1,
            props: {
                isRoot: true,
            },
        })).toNumber() > 0);
    }
    async refreshAuthToken(data) {
        let userAuth = await this.findOneBy({
            query: {
                projectId: data.projectId,
                userId: data.userId,
                workspaceType: data.workspaceType,
            },
            select: {
                _id: true,
            },
            props: {
                isRoot: true,
            },
        });
        if (!userAuth) {
            userAuth = new Model();
            userAuth.projectId = data.projectId;
            userAuth.userId = data.userId;
            userAuth.authToken = data.authToken;
            userAuth.workspaceType = data.workspaceType;
            userAuth.workspaceUserId = data.workspaceUserId;
            userAuth.miscData = data.miscData;
            await this.create({
                data: userAuth,
                props: {
                    isRoot: true,
                },
            });
        }
        else {
            await this.updateOneById({
                id: userAuth.id,
                data: {
                    authToken: data.authToken,
                    workspaceUserId: data.workspaceUserId,
                    miscData: data.miscData,
                },
                props: {
                    isRoot: true,
                },
            });
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "getUserAuth", 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=WorkspaceUserAuthTokenService.js.map