Add entity customer numbers and scope APIs via selected company.
Entity gets an optional unique-per-company number with allocate/peek/isNumberFree helpers. Company-scoped store methods no longer take companyId and resolve google_auth.selected_company instead.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { CREATE, GET_NEXT_SORT } from '../../sql/entitycontact.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
function shape(row) {
|
||||
@@ -20,14 +20,14 @@ function shape(row) {
|
||||
*
|
||||
* @example
|
||||
* const fn = create(pool);
|
||||
* const contact = await fn(42, 1, 5, { contactType: 'email', contact: 'a@b.c' });
|
||||
* const contact = await fn(42, 5, { contactType: 'email', contact: 'a@b.c' });
|
||||
* // => { id, entityId, contactType, contact, sort }
|
||||
*/
|
||||
export default function create(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId, input = {}) => {
|
||||
return async (googleAuthId, entityId, input = {}) => {
|
||||
const contactType = input.contactType?.trim?.()
|
||||
? input.contactType.trim()
|
||||
: null;
|
||||
@@ -36,7 +36,7 @@ export default function create(pool) {
|
||||
throw new Error('Contact required');
|
||||
}
|
||||
|
||||
await access(googleAuthId, companyId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
let sort = input.sort;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DELETE_BY_ENTITY_AND_ID } from '../../sql/entitycontact.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
/**
|
||||
@@ -10,18 +10,18 @@ import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
*
|
||||
* @example
|
||||
* const fn = del(pool);
|
||||
* await fn(42, 1, 5, 9);
|
||||
* await fn(42, 5, 9);
|
||||
*/
|
||||
export default function del(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId, id) => {
|
||||
return async (googleAuthId, entityId, id) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, companyId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
const result = await pool
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LIST_BY_ENTITY } from '../../sql/entitycontact.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
function shape(row) {
|
||||
@@ -20,15 +20,15 @@ function shape(row) {
|
||||
*
|
||||
* @example
|
||||
* const fn = list(pool);
|
||||
* const contacts = await fn(42, 1, 5);
|
||||
* const contacts = await fn(42, 5);
|
||||
* // => [{ id, entityId, contactType, contact, sort }]
|
||||
*/
|
||||
export default function list(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId) => {
|
||||
await access(googleAuthId, companyId);
|
||||
return async (googleAuthId, entityId) => {
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
const result = await pool
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { GET_BY_ENTITY_AND_ID, UPDATE } from '../../sql/entitycontact.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
function shape(row) {
|
||||
@@ -20,19 +20,19 @@ function shape(row) {
|
||||
*
|
||||
* @example
|
||||
* const fn = update(pool);
|
||||
* const contact = await fn(42, 1, 5, 9, { contact: 'b@c.d' });
|
||||
* const contact = await fn(42, 5, 9, { contact: 'b@c.d' });
|
||||
* // => { id, entityId, contactType, contact, sort }
|
||||
*/
|
||||
export default function update(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId, id, patch = {}) => {
|
||||
return async (googleAuthId, entityId, id, patch = {}) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, companyId);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
const existing = await pool
|
||||
|
||||
Reference in New Issue
Block a user