ENTITY CONFIGURATION MODEL:
export interface ScaleInput {
scale: number;
value: string;
}
export interface EntityConfiguration {
name: string;
confidentialityScale: ScaleInput[];
}
class EntityConfigurationModel {
private static schema: Schema = new Schema(
{
name: { type: String, required: true, index: true, unique: true },
confidentialityScale: {
type: [ScaleInputSchema],
validate: {
validator: uniqueScaleValuesValidator,
message: '{PATH} items must be unique.',
},
},
},
{
timestamps: true,
},
);
public static model = model<EntityConfiguration>(
'EntityConfiguration',
EntityConfigurationModel.schema,
);
}
export default EntityConfigurationModel;
ASSET INVENTORY MODEL:
export interface AssetInventory {
assetName: string;
confidentialityScale: Types.ObjectId;
}
class AssetInventoryModel {
private static schema: Schema = new Schema(
{
assetName: { type: String, required: true, index: true, unique: true },
confidentialityScale: {
type: Schema.Types.ObjectId,
ref: 'EntityConfiguration',
},
}
},
{ timestamps: true },
);
public static model = model<AssetInventory>(
'AssetInventory',
AssetInventoryModel.schema,
);
}
export default AssetInventoryModel;
STRUCTURE OF THE ENTITY CONFIGURATION IN THE DB:
{
“_id”: “6601e3bfea080672c7bc097d”,
“name”: “Sample Entity”,
“confidentialityScale”: [
{
“scale”: 2,
“value”: “High”,
“_id”: “6601e3bfea080672c7bc097e”
},
{
“scale”: 1,
“value”: “Low”,
“_id”: “6601e3bfea080672c7bc097f”
}
],
“createdAt”: “2024-03-25T20:51:11.140Z”,
“updatedAt”: “2024-03-25T20:51:11.140Z”,
“__v”: 0
},
WHEN TRYING TO CREATE AN ASSET INVENTORY, I’M SENDING THIS DATA:
{
“assetName”: “Sample Asset Name”,
“confidentialityScale”: “6601e3bfea080672c7bc097e”,
}
GET ALL ASSET INVENTORY FUNCTION IN ASSET INVENTORY SERVICE:
async findAll() {
try {
const assetInventory = await this.assetInventoryRepository.findPaginated(
10,
1,
{},
);
console.log(assetInventory);
return assetInventory;
} catch (error) {
throw new BadRequestException(error.message);
}
}
NOW THE PROBLEM IS WHEN I TRY TO GET ALL ASSET INVENTORY, I DON’T WANT IT TO JUST RETURN THE CONFIDENTIALITY SCALE ID LIKE THIS:
{
“data”: [
{
“_id”: “661b946826e35108518a5e7d”,
“assetName”: “Sample Asset”,
“confidentialityScale”: “6601e3bfea080672c7bc097e”,
“__v”: 0
}
],
“pagination”: {
“total”: 1,
“pageSize”: 10,
“currentPage”: 1
}
}
I WANT IT TO RETURN THE ACTUAL DATA ASSOCIATED WITH THAT ID SO THE RESPONSE SHOULD LOOK LIKE THIS AFTER RETURNING:
{
“data”: [
{
“_id”: “661b946826e35108518a5e7d”,
“assetName”: “Sample Asset”,
“confidentialityScale”: {
“scale”: 2,
“value”: “High”,
“_id”: “6601e3bfea080672c7bc097e”
},
“__v”: 0
}
],
“pagination”: {
“total”: 1,
“pageSize”: 10,
“currentPage”: 1
}
}
I HAVE TRIED SO MANY THINGS. POPULATING IT DOES NOT WORK, IT RETURNS NULL. I’M GUESSING BECAUSE THE CONFIDENTIALITY SCALE IS NESTED IN THE ENTITY CONFIGURATION