u
This commit is contained in:
75
src/store/payment/create.js
Normal file
75
src/store/payment/create.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import { queriesFor, requireId, requireText } from '../logistics/helpers.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
accountId: row.account_id,
|
||||
manifestId: row.manifest_id,
|
||||
type: row.type,
|
||||
amount: row.amount,
|
||||
};
|
||||
}
|
||||
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const manifestAccess = resolveManifestAccess(provider);
|
||||
const q = queriesFor(provider);
|
||||
|
||||
/**
|
||||
* Adds a payment to a manifest (order) using an account of the selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {object} input - Payment input.
|
||||
* @param {number} input.accountId - The account id.
|
||||
* @param {number} input.manifestId - The manifest (order) id.
|
||||
* @param {string} input.type - Payment type.
|
||||
* @param {number} input.amount - Payment amount.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, accountId: number, manifestId: number, type: string, amount: number}>}
|
||||
* @throws {Error} If account or manifest is not found, or input is invalid.
|
||||
*
|
||||
* @example
|
||||
* await store.payment.create(googleAuthId, { accountId: 1, manifestId: 42, type: 'cash', amount: 19.9 });
|
||||
*/
|
||||
return async (googleAuthId, input = {}, tx) => {
|
||||
const accountId = requireId(input.accountId, 'Account');
|
||||
const manifestId = requireId(input.manifestId, 'Manifest');
|
||||
const type = requireText(input.type, 'Payment type');
|
||||
if (!Number.isFinite(input.amount)) {
|
||||
throw new Error('Invalid payment amount');
|
||||
}
|
||||
|
||||
const run = async (runner, innerTx) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const accountRes = await runner.query(q.CHECK_ACCOUNT_COMPANY, { accountId, companyId });
|
||||
if (!accountRes.rows[0]) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
await manifestAccess(companyId, manifestId, innerTx);
|
||||
|
||||
const result = await runner.query(q.PAYMENT_CREATE, {
|
||||
accountId,
|
||||
manifestId,
|
||||
type,
|
||||
amount: input.amount,
|
||||
});
|
||||
const row = result.rows[0] ?? {
|
||||
id: result.insertId,
|
||||
account_id: accountId,
|
||||
manifest_id: manifestId,
|
||||
type,
|
||||
amount: input.amount,
|
||||
};
|
||||
return shape(row);
|
||||
};
|
||||
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction((runner, innerTx) => run(runner, innerTx));
|
||||
}
|
||||
return run(makeRunner(provider, tx));
|
||||
};
|
||||
}
|
||||
26
src/store/payment/delete.js
Normal file
26
src/store/payment/delete.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { queriesFor, requireId } from '../logistics/helpers.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
/**
|
||||
* Deletes a payment belonging to a manifest of the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The payment id.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
*
|
||||
* @example
|
||||
* await store.payment.delete(googleAuthId, 7);
|
||||
*/
|
||||
export default function del(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const q = queriesFor(provider);
|
||||
|
||||
return async (googleAuthId, id, tx) => {
|
||||
const paymentId = requireId(id, 'Payment');
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
await runner.query(q.PAYMENT_DELETE, { id: paymentId, companyId });
|
||||
};
|
||||
}
|
||||
37
src/store/payment/listByManifestId.js
Normal file
37
src/store/payment/listByManifestId.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import { queriesFor, requireId } from '../logistics/helpers.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveManifestAccess from '../manifestattachment/resolveManifestAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
/**
|
||||
* Lists payments of a manifest (order) in the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} manifestId - The manifest id.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{id: number, accountId: number, manifestId: number, type: string, amount: number}>>}
|
||||
*
|
||||
* @example
|
||||
* const payments = await store.payment.listByManifestId(googleAuthId, 42);
|
||||
*/
|
||||
export default function listByManifestId(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const manifestAccess = resolveManifestAccess(provider);
|
||||
const q = queriesFor(provider);
|
||||
|
||||
return async (googleAuthId, manifestIdInput, tx) => {
|
||||
const manifestId = requireId(manifestIdInput, 'Manifest');
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
await manifestAccess(companyId, manifestId, tx);
|
||||
|
||||
const result = await runner.query(q.PAYMENT_LIST_BY_MANIFEST, { manifestId });
|
||||
return result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
accountId: row.account_id,
|
||||
manifestId: row.manifest_id,
|
||||
type: row.type,
|
||||
amount: row.amount,
|
||||
}));
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user