feat: activate/inactive contacts.

This commit is contained in:
a.bouhuolia
2021-08-06 14:03:55 +02:00
parent f69ab7243e
commit 34377c8e53
3 changed files with 130 additions and 5 deletions

View File

@@ -12,13 +12,10 @@ import {
IContactsAutoCompleteFilter,
} from 'interfaces';
import JournalPoster from '../Accounting/JournalPoster';
import { ERRORS } from './constants';
type TContactService = 'customer' | 'vendor';
const ERRORS = {
OPENING_BALANCE_DATE_REQUIRED: 'OPENING_BALANCE_DATE_REQUIRED',
};
@Service()
export default class ContactsService {
@Inject()
@@ -361,4 +358,34 @@ export default class ContactsService {
}
);
}
/**
* Inactive the given contact.
* @param {number} tenantId - Tenant id.
* @param {number} contactId - Contact id.
*/
async inactivateContact(tenantId: number, contactId: number): Promise<void> {
const { Contact } = this.tenancy.models(tenantId);
const contact = await this.getContactByIdOrThrowError(tenantId, contactId);
if(!contact.active) {
throw new ServiceError(ERRORS.CONTACT_ALREADY_INACTIVE);
}
await Contact.query().findById(contactId).update({ active: false });
}
/**
* Inactive the given contact.
* @param {number} tenantId - Tenant id.
* @param {number} contactId - Contact id.
*/
async activateContact(tenantId: number, contactId: number): Promise<void> {
const { Contact } = this.tenancy.models(tenantId);
const contact = await this.getContactByIdOrThrowError(tenantId, contactId);
if(contact.active) {
throw new ServiceError(ERRORS.CONTACT_ALREADY_ACTIVE);
}
await Contact.query().findById(contactId).update({ active: true });
}
}

View File

@@ -20,3 +20,10 @@ export const DEFAULT_VIEWS = [
columns: DEFAULT_VIEW_COLUMNS,
},
];
export const ERRORS = {
OPENING_BALANCE_DATE_REQUIRED: 'OPENING_BALANCE_DATE_REQUIRED',
CONTACT_ALREADY_INACTIVE: 'CONTACT_ALREADY_INACTIVE',
CONTACT_ALREADY_ACTIVE: 'CONTACT_ALREADY_ACTIVE'
};