Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function encryptPasswordInRequestData(
reqData: GeneralUserEntityRequestData,
passwordPropName: DocumentPath,
encrypt: EncryptFunction
): GeneralUserEntityRequestData {
switch (reqData.method) {
case "insertOne":
case "insertAndGet": {
const { payload } = reqData;
const { value } = payload;
const password = getNestedValue(value, passwordPropName);
if (password) {
const { $set, $docPath } = $bind();
const valueWithEncryptedPass = update(
value,
// @ts-ignore password always exists
$set($docPath(passwordPropName), encrypt(password))
);
const { $set: $OtherSet, $docPath: $otherDocPath } = $bind<
typeof reqData
>();
return update(
reqData,
$OtherSet($otherDocPath("payload", "value"), valueWithEncryptedPass)
);
} else {
const valuesWithEncryptedPass = values.map(value => {
const password = getNestedValue(value, passwordPropName);
if (password) {
const { $set, $docPath } = $bind();
return update(
value,
// @ts-ignore password always exists
$set($docPath(passwordPropName), encrypt(password))
);
} else {
return value;
}
});
const { $set, $docPath } = $bind();
Object.keys(operation).forEach((key: any) => {
// @ts-ignore: Partial has no index signature.
const password = getNestedValue(operation[key], passwordPropName);
if (password) {
const { $set, $docPath } = $bind();
operationWithEncryptedPass = update(
operationWithEncryptedPass,
$set($docPath(key, passwordPropName), encrypt(password))
);
}
});
static getHeadEntity>(
state: LocalStateOf,
query: IdQuery
): Entity {
const { entityName, id } = query;
if (state.entities[entityName] == null)
throw new Error(
`LocalStateFinder#getHeadEntity(). No entityName found: "${entityName}".`
);
const entityInfo = getNestedValue(
state,
createDocumentPath("entities", entityName, id)
);
if (entityInfo == null)
throw new Error(
`LocalStateFinder#getHeadEntity(). No id found in entityName: "${entityName}". id: "${id}".`
); // If no head, meaning origin is the head (= commits.length === 0)
return entityInfo.head ? entityInfo.head : entityInfo.origin;
}
/**
static hasEntity>(
state: LocalStateOf,
query: IdQuery
): boolean {
const { entityName, id } = query;
if (!state.entities[entityName]) return false;
const entityInfo = getNestedValue(
state,
createDocumentPath("entities", entityName, id)
);
return entityInfo != null;
}
/**
async getForeignEntity>(
entity: E,
foreign: ForeignQueryParams
): Promise {
const { documentPath, entityName } = foreign;
try {
const foreignId = getNestedValue(entity, documentPath);
const result = await this.entityClient.get({ id: foreignId, entityName });
return result.entity;
} catch (e) {
e.message = `Error while getting entities "${entityName}" by foreign keys "${documentPath}".\n${
e.message
}`;
throw e;
}
}
}
const foreignIds = entities.map(entity =>
getNestedValue(entity, documentPath)
);
static getEntityInfo>(
state: LocalStateOf,
query: IdQuery
): LocalEntityInfo {
const { entityName, id } = query;
if (state.entities[entityName] == null)
throw new Error(
`LocalStateFinder#getEntityInfo(). No entityName found: "${entityName}".`
);
const entityInfo = getNestedValue(
state,
createDocumentPath("entities", entityName, id)
);
if (entityInfo == null)
throw new Error(
`LocalStateFinder#getEntityInfo(). No id found in entityName: "${entityName}". id: "${id}".`
);
return entityInfo;
}
}