All files / src/tests models.ts

96.19% Statements 101/105
100% Branches 5/5
83.33% Functions 5/6
96.19% Lines 101/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 1061x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { type Mask } from '../databases/mongo/services/maskService'
 
import { type MongoDaoParsed } from '../databases/mongo/types/mongoDbTypes'
import { UserModels, OrgModels, Organization, User } from './jestHelpers'
import { _, Definition } from 'good-cop'
 
 
const validationAddr = () => ({
    user: _.object({
        _id: _.string(),
        name: _.string(),
        password: _.string(),
        organization: _.ref('organization'),
    }),
    organization: _.object({
        _id: _.string(),
        name: _.string(),
        adminField1: _.string(),
        adminField2: _.string(),
        anotherFieldUserHaveNoAccess: _.string(),
        teams: _.array({
            users: _.array(_.ref('user')),
            name: _.string(),
            adminAuth: _.string()
        })
    }),
})
 
const createDaoParsed = <T = any>(o: Partial<Mask<T>>) => {
    return {
        priority: 50,
        for: ['ALL'],
        on: ['getAll', 'getOne', 'create', 'update', 'delete'],
        ...o
    } as any as Mask
}
 
type Models = {
    mongo: { [dbId: string]: { [modelName: string]: Record<string, any> } }, // do not refactor type here because less readable on intellisense
    daos: { [dbId: string]: { [modelName: string]: MongoDaoParsed<any> } },
    populateAddrFlatWithModelName: { [dbId: string]: { [modelName: string]: { [populatedFieldNameFlat: string]: string } } },
    validation: { [dbId: string]: { [modelName: string]: Definition } },
}
 
export const models: Models = {
    mongo: {},
    daos: {
        mainDb: {
            user: {
                type: 'mongo',
                // authorizedOLDApiEndpoint: ['count', 'create', 'delete', 'deleteWithFilter', 'getAll', 'getById', 'getFirstN', 'getOne', 'update', 'delete'],
                modelConfig: {},
                expose: [],
                mask: [createDaoParsed<User<any>>({
                    // mask password field for read and all admin and users
                    for: [{ role: 'user' as any }, { role: 'admin' as any }],
                    on: ['getAll', 'getOne'],
                    mask: () => ({
                        password: true,
                    })
                }), createDaoParsed<User<any>>({
                    // mask everything for public except name (this is not realistic since we should use 'expose' or 'filter' instead)
                    for: [{ role: 'public' }],
                    select: () => ({ name: true })
                })],
                populate: [],
            } satisfies MongoDaoParsed<UserModels>,
            organization: {
                type: 'mongo',
                // authorizedOLDApiEndpoint: ['count', 'create', 'delete', 'deleteWithFilter', 'getAll', 'getById', 'getFirstN', 'getOne', 'update', 'delete'],
                modelConfig: {},
                expose: [],
                mask: [createDaoParsed<Organization<any>>({
                    // mask ADMIN fields for users all methods
                    for: [{ role: 'user' as any }],
                    mask: () => ({
                        'admin*': true,
                        anotherFieldUserHaveNoAccess: true,
                        teams: [{ 'adm*th': true, name: true }] // mask array type fields, should match adminAuth but not the rest
                    })
                }), createDaoParsed<Organization<any>>({
                    // mask ADMIN fields for users all methods
                    for: [{ role: 'user' as any }],
                    select: () => ({
                        name: true,
                        anotherFieldUserHaveNoAccess: true, // so this should not be masked in the end while admin fields should be
                        teams: [{ name: true, users: true }] // name is supposed to be in the final result
                    })
                }),],
                populate: [],
            } satisfies MongoDaoParsed<OrgModels>
        }
    },
    populateAddrFlatWithModelName: {
        mainDb: {
            user: {
                organization: 'organization'
            },
            organization: {
                'teams[0].users': 'user'
            },
        }
    },
    validation: { mainDb: validationAddr() as any },
}