feat: implement ARCA integration architecture including credential, fiscal profile, and invoice models, services, and onboarding flows
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { Document, FilterQuery, Model, Schema, model } from "mongoose";
|
||||
import {
|
||||
ARCA_CREDENTIAL_STATUS,
|
||||
CreateArcaCredentialParams,
|
||||
FindArcaCredentialsParams,
|
||||
IArcaCredential,
|
||||
IArcaCredentialsAdapter,
|
||||
UpdateArcaCredentialParams,
|
||||
} from "./ArcaCredentials.Interface";
|
||||
|
||||
export interface IArcaCredentialDocument extends Omit<IArcaCredential, "id">, Document {}
|
||||
|
||||
export class ArcaCredentialsAdapterMongoose implements IArcaCredentialsAdapter {
|
||||
schema: Schema;
|
||||
arcaCredentialList: Model<IArcaCredentialDocument>;
|
||||
|
||||
constructor() {
|
||||
this.schema = new Schema({
|
||||
companyId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
required: true,
|
||||
ref: "Company",
|
||||
unique: true,
|
||||
index: true,
|
||||
},
|
||||
csrPem: { type: String, required: false },
|
||||
certificatePem: { type: String, required: false },
|
||||
encryptedPrivateKey: { type: String, required: true },
|
||||
privateKeyIv: { type: String, required: true },
|
||||
privateKeyAuthTag: { type: String, required: true },
|
||||
encryptionKeyVersion: { type: String, required: false, default: "v1" },
|
||||
status: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: Object.values(ARCA_CREDENTIAL_STATUS),
|
||||
index: true,
|
||||
},
|
||||
certificateValidFrom: { type: Date, required: false },
|
||||
certificateValidTo: { type: Date, required: false },
|
||||
lastValidationError: { type: String, required: false },
|
||||
createdAt: { type: Date, required: true, default: Date.now },
|
||||
updatedAt: { type: Date, required: true, default: Date.now },
|
||||
});
|
||||
|
||||
this.arcaCredentialList = model<IArcaCredentialDocument>("ArcaCredential", this.schema);
|
||||
}
|
||||
|
||||
public async create(data: CreateArcaCredentialParams): Promise<IArcaCredential> {
|
||||
return await this.arcaCredentialList.create(data);
|
||||
}
|
||||
|
||||
private buildSearchCriteria(filters: FindArcaCredentialsParams): FilterQuery<IArcaCredential> {
|
||||
const searchCriteria: FilterQuery<IArcaCredential> = {};
|
||||
|
||||
if (filters.id) searchCriteria._id = filters.id;
|
||||
if (filters.companyId) searchCriteria.companyId = filters.companyId;
|
||||
if (filters.status) searchCriteria.status = filters.status;
|
||||
|
||||
return searchCriteria;
|
||||
}
|
||||
|
||||
public async find(filters: FindArcaCredentialsParams): Promise<IArcaCredential[]> {
|
||||
const searchCriteria = this.buildSearchCriteria(filters);
|
||||
return this.arcaCredentialList.find(searchCriteria).sort({ createdAt: -1 }).exec();
|
||||
}
|
||||
|
||||
public async findOne(filters: FindArcaCredentialsParams): Promise<IArcaCredentialDocument | null> {
|
||||
const searchCriteria = this.buildSearchCriteria(filters);
|
||||
return this.arcaCredentialList.findOne(searchCriteria).exec();
|
||||
}
|
||||
|
||||
public async update(data: UpdateArcaCredentialParams): Promise<IArcaCredentialDocument | null> {
|
||||
const { id, companyId, sessionUser, ...updateData } = data;
|
||||
|
||||
if (!id && !companyId) {
|
||||
throw new Error("Se requiere un identificador para actualizar las credenciales ARCA");
|
||||
}
|
||||
|
||||
return this.arcaCredentialList
|
||||
.findOneAndUpdate(this.buildSearchCriteria({ id, companyId, sessionUser }), updateData, { new: true })
|
||||
.exec();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user