Skip to content

Commit

Permalink
update vault indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratap2018 committed Jul 12, 2024
1 parent 81e4367 commit c4360f6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/user-vault/dto/document.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class IIndexed{
@ApiProperty()
hmac:Ihmac;

@ApiProperty()
@ApiProperty({
})
attributes:IAttributes;
}

Expand Down
5 changes: 5 additions & 0 deletions src/user-vault/repository/document.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class DocumentRepository {
sizeInbytes: Buffer.byteLength(JSON.stringify(doc.toObject())),
};
}
async getDocuments(documentFilterQuery: FilterQuery<DocSchema>) {
const doc = await this.documentModel.find(documentFilterQuery);

return doc;
}

async deleteDocument(documentFilterQuery: FilterQuery<DocSchema>) {
const doc = await this.documentModel.deleteOne(documentFilterQuery);
Expand Down
1 change: 0 additions & 1 deletion src/user-vault/schemas/doucment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,5 @@ export class DocSchema {
export const DoucmentSchema = SchemaFactory.createForClass(DocSchema);

DoucmentSchema.index({ "id": 1 }, { unique: true, background:true, });
DoucmentSchema.index({ 'indexed.attributes.0.value': 1 }, { unique: true });


20 changes: 19 additions & 1 deletion src/user-vault/services/user-vault.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ export class UserVaultService {
}

// await this.documentRepository.connectDB(docVaultId);
try {
const attr = document.indexed[0].attributes as any;
const filterd = attr.filter((e) => {
if (e.unique == true) {
return e;
}
});

const data = await this.documentRepository.getDocuments({
'indexed.attributes': filterd,
});

if (data.length > 0) {
throw new Error('Unique index alreday exists');
}
} catch (error) {
throw Error(error);
}

const doc = await this.documentRepository.createDocument(document);
// await this.documentRepository.disconnectDB();

Expand Down Expand Up @@ -303,7 +322,6 @@ export class UserVaultService {
const result = await this.documentRepository.deleteDocument({
id: documentId,
});
console.log(result);

return { message: 'document deleted', result: result };
}
Expand Down

0 comments on commit c4360f6

Please sign in to comment.