2
0
mirror of https://github.com/frappe/books.git synced 2024-09-19 11:09:01 +00:00
books/backend/helpers.ts
18alantom 43784984c3 fix: stock movement
- test: stock movement create and cancel
2022-11-07 13:28:35 +05:30

82 lines
1.7 KiB
TypeScript

import { constants } from 'fs';
import fs from 'fs/promises';
import { DatabaseMethod } from 'utils/db/types';
import { CUSTOM_EVENTS } from 'utils/messages';
import { KnexColumnType } from './database/types';
export const sqliteTypeMap: Record<string, KnexColumnType> = {
AutoComplete: 'text',
Currency: 'text',
Int: 'integer',
Float: 'float',
Percent: 'float',
Check: 'boolean',
Code: 'text',
Date: 'date',
Datetime: 'datetime',
Time: 'time',
Text: 'text',
Data: 'text',
Link: 'text',
DynamicLink: 'text',
Password: 'text',
Select: 'text',
Attachment: 'text',
AttachImage: 'text',
Color: 'text',
};
export const SYSTEM = '__SYSTEM__';
export const validTypes = Object.keys(sqliteTypeMap);
export function getDefaultMetaFieldValueMap() {
const now = new Date().toISOString();
return {
createdBy: SYSTEM,
modifiedBy: SYSTEM,
created: now,
modified: now,
};
}
export const databaseMethodSet: Set<DatabaseMethod> = new Set([
'insert',
'get',
'getAll',
'getSingleValues',
'rename',
'update',
'delete',
'deleteAll',
'close',
'exists',
]);
export function emitMainProcessError(
error: unknown,
more?: Record<string, unknown>
) {
(process.emit as Function)(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, error, more);
}
export async function checkFileAccess(filePath: string, mode?: number) {
mode ??= constants.W_OK;
return await fs
.access(filePath, mode)
.then(() => true)
.catch(() => false);
}
export async function unlinkIfExists(filePath: unknown) {
if (!filePath || typeof filePath !== 'string') {
return false;
}
const exists = await checkFileAccess(filePath);
if (exists) {
await fs.unlink(filePath);
return true;
}
return false;
}