numerus/test/edit_contact.sql

163 lines
6.4 KiB
MySQL
Raw Normal View History

-- Test edit_contact
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(19);
set search_path to auth, numerus, public;
select has_function('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]']);
select function_lang_is('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'plpgsql');
select function_returns('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'uuid');
select isnt_definer('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]']);
select volatility_is('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'volatile');
select function_privs_are('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_contact', array ['uuid', 'text', 'text', 'text', 'text', 'tax_details', 'text', 'text', 'tag_name[]'], 'authenticator', array []::text[]);
set client_min_messages to warning;
truncate contact_swift cascade;
truncate contact_iban cascade;
truncate contact_web cascade;
truncate contact_email cascade;
truncate contact_phone cascade;
truncate contact_tax_details cascade;
truncate contact cascade;
truncate payment_method cascade;
truncate company cascade;
reset client_min_messages;
set constraints "company_default_payment_method_id_fkey" deferred;
insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, default_payment_method_id)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 111)
;
insert into payment_method (payment_method_id, company_id, name, instructions)
values (111, 1, 'cash', 'cash')
, (112, 1, 'bank', 'send money to my bank account')
;
set constraints "company_default_payment_method_id_fkey" immediate;
Split contact relation into tax_details, phone, web, and email We need to have contacts with just a name: we need to assign freelancer’s quote as expense linked the government, but of course we do not have a phone or email for that “contact”, much less a VATIN or other tax details. It is also interesting for other expenses-only contacts to not have to input all tax details, as we may not need to invoice then, thus are useless for us, but sometimes it might be interesting to have them, “just in case”. Of course, i did not want to make nullable any of the tax details required to generate an invoice, otherwise we could allow illegal invoices. Therefore, that data had to go in a different relation, and invoice’s foreign key update to point to that relation, not just customer, or we would again be able to create invalid invoices. We replaced the contact’s trade name with just name, because we do not need _three_ names for a contact, but we _do_ need two: the one we use to refer to them and the business name for tax purposes. The new contact_phone, contact_web, and contact_email relations could be simply a nullable field, but i did not see the point, since there are not that many instances where i need any of this data. Now company.taxDetailsForm is no longer “the same as contactForm with some extra fields”, because i have to add a check whether the user needs to invoice the contact, to check that the required values are there. I have an additional problem with the contact form when not using JavaScript: i must set the required field to all tax details fields to avoid the “(optional)” suffix, and because they _are_ required when that checkbox is enabled, but i can not set them optional when the check is unchecked. My solution for now is to ignore the form validation, and later i will add some JavaScript that adds the validation again, so it will work in all cases.
2023-06-30 19:32:48 +00:00
insert into contact (contact_id, company_id, slug, name, tags)
values (12, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'Contact 1', '{tag1}')
, (13, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'Contact 2', '{tag2}')
, (14, 1, '12fd031b-8f4d-4ac1-9dde-7df336dc6d52', 'Contact 3', '{tag3}')
;
insert into contact_tax_details (contact_id, business_name, vatin, address, city, province, postal_code, country_code)
values (12, 'Contact 1 Ltd', 'XX555', '', '', '', '', 'ES')
, (13, 'Contact 2 Ltd', 'XX666', '', '', '', '', 'ES')
;
insert into contact_phone (contact_id, phone)
values (12, '777-777-777')
, (13, '888-888-888')
;
insert into contact_email (contact_id, email)
values (12, 'c@c')
, (13, 'd@d')
;
insert into contact_web (contact_id, uri)
values (12, 'https://1/')
, (13, 'https://2/')
;
insert into contact_iban (contact_id, iban)
values (12, 'NL04RABO9373475770')
, (13, 'NL17RABO4416709382')
;
insert into contact_swift (contact_id, bic)
values (12, 'ABNANL2A')
, (13, 'ARBNNL22')
;
select lives_ok(
$$ select edit_contact('7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'Contact 2.1', '999-999-999', 'c1@c1', 'https://c', '(Contact 2.1 Ltd,40404040D,"Fake St., 123",City 2.1,Province 2.1,19486,ES)', 'NL68RABO7792285766', '', array['tag1']) $$,
'Should be able to edit the first contact'
);
select lives_ok(
$$ select edit_contact('b57b980b-247b-4be4-a0b7-03a7819c53ae', 'Contact 2.2', '', '', '', null, '', 'ASNBNL21', array['tag1', 'tag3']) $$,
'Should be able to edit the second contact'
);
Split contact relation into tax_details, phone, web, and email We need to have contacts with just a name: we need to assign freelancer’s quote as expense linked the government, but of course we do not have a phone or email for that “contact”, much less a VATIN or other tax details. It is also interesting for other expenses-only contacts to not have to input all tax details, as we may not need to invoice then, thus are useless for us, but sometimes it might be interesting to have them, “just in case”. Of course, i did not want to make nullable any of the tax details required to generate an invoice, otherwise we could allow illegal invoices. Therefore, that data had to go in a different relation, and invoice’s foreign key update to point to that relation, not just customer, or we would again be able to create invalid invoices. We replaced the contact’s trade name with just name, because we do not need _three_ names for a contact, but we _do_ need two: the one we use to refer to them and the business name for tax purposes. The new contact_phone, contact_web, and contact_email relations could be simply a nullable field, but i did not see the point, since there are not that many instances where i need any of this data. Now company.taxDetailsForm is no longer “the same as contactForm with some extra fields”, because i have to add a check whether the user needs to invoice the contact, to check that the required values are there. I have an additional problem with the contact form when not using JavaScript: i must set the required field to all tax details fields to avoid the “(optional)” suffix, and because they _are_ required when that checkbox is enabled, but i can not set them optional when the check is unchecked. My solution for now is to ignore the form validation, and later i will add some JavaScript that adds the validation again, so it will work in all cases.
2023-06-30 19:32:48 +00:00
select lives_ok(
$$ select edit_contact('12fd031b-8f4d-4ac1-9dde-7df336dc6d52', 'Contact 2.3', '111-111-111', 'd2@d2', 'https://d', '(Contact 2.3 Ltd,41414141L,"Another Fake St., 123",City 2.2,Province 2.2,17417,ES)', 'NL48RABO6482008283', 'BOFSNL21002', array['tag2']) $$,
Split contact relation into tax_details, phone, web, and email We need to have contacts with just a name: we need to assign freelancer’s quote as expense linked the government, but of course we do not have a phone or email for that “contact”, much less a VATIN or other tax details. It is also interesting for other expenses-only contacts to not have to input all tax details, as we may not need to invoice then, thus are useless for us, but sometimes it might be interesting to have them, “just in case”. Of course, i did not want to make nullable any of the tax details required to generate an invoice, otherwise we could allow illegal invoices. Therefore, that data had to go in a different relation, and invoice’s foreign key update to point to that relation, not just customer, or we would again be able to create invalid invoices. We replaced the contact’s trade name with just name, because we do not need _three_ names for a contact, but we _do_ need two: the one we use to refer to them and the business name for tax purposes. The new contact_phone, contact_web, and contact_email relations could be simply a nullable field, but i did not see the point, since there are not that many instances where i need any of this data. Now company.taxDetailsForm is no longer “the same as contactForm with some extra fields”, because i have to add a check whether the user needs to invoice the contact, to check that the required values are there. I have an additional problem with the contact form when not using JavaScript: i must set the required field to all tax details fields to avoid the “(optional)” suffix, and because they _are_ required when that checkbox is enabled, but i can not set them optional when the check is unchecked. My solution for now is to ignore the form validation, and later i will add some JavaScript that adds the validation again, so it will work in all cases.
2023-06-30 19:32:48 +00:00
'Should be able to edit the third contact'
);
select bag_eq(
Split contact relation into tax_details, phone, web, and email We need to have contacts with just a name: we need to assign freelancer’s quote as expense linked the government, but of course we do not have a phone or email for that “contact”, much less a VATIN or other tax details. It is also interesting for other expenses-only contacts to not have to input all tax details, as we may not need to invoice then, thus are useless for us, but sometimes it might be interesting to have them, “just in case”. Of course, i did not want to make nullable any of the tax details required to generate an invoice, otherwise we could allow illegal invoices. Therefore, that data had to go in a different relation, and invoice’s foreign key update to point to that relation, not just customer, or we would again be able to create invalid invoices. We replaced the contact’s trade name with just name, because we do not need _three_ names for a contact, but we _do_ need two: the one we use to refer to them and the business name for tax purposes. The new contact_phone, contact_web, and contact_email relations could be simply a nullable field, but i did not see the point, since there are not that many instances where i need any of this data. Now company.taxDetailsForm is no longer “the same as contactForm with some extra fields”, because i have to add a check whether the user needs to invoice the contact, to check that the required values are there. I have an additional problem with the contact form when not using JavaScript: i must set the required field to all tax details fields to avoid the “(optional)” suffix, and because they _are_ required when that checkbox is enabled, but i can not set them optional when the check is unchecked. My solution for now is to ignore the form validation, and later i will add some JavaScript that adds the validation again, so it will work in all cases.
2023-06-30 19:32:48 +00:00
$$ select company_id, name, tags, created_at from contact $$,
$$ values (1, 'Contact 2.1', '{tag1}'::tag_name[], CURRENT_TIMESTAMP)
, (1, 'Contact 2.2', '{tag1,tag3}'::tag_name[], CURRENT_TIMESTAMP)
, (1, 'Contact 2.3', '{tag2}'::tag_name[], CURRENT_TIMESTAMP)
$$,
'Should have updated all contacts'
);
Split contact relation into tax_details, phone, web, and email We need to have contacts with just a name: we need to assign freelancer’s quote as expense linked the government, but of course we do not have a phone or email for that “contact”, much less a VATIN or other tax details. It is also interesting for other expenses-only contacts to not have to input all tax details, as we may not need to invoice then, thus are useless for us, but sometimes it might be interesting to have them, “just in case”. Of course, i did not want to make nullable any of the tax details required to generate an invoice, otherwise we could allow illegal invoices. Therefore, that data had to go in a different relation, and invoice’s foreign key update to point to that relation, not just customer, or we would again be able to create invalid invoices. We replaced the contact’s trade name with just name, because we do not need _three_ names for a contact, but we _do_ need two: the one we use to refer to them and the business name for tax purposes. The new contact_phone, contact_web, and contact_email relations could be simply a nullable field, but i did not see the point, since there are not that many instances where i need any of this data. Now company.taxDetailsForm is no longer “the same as contactForm with some extra fields”, because i have to add a check whether the user needs to invoice the contact, to check that the required values are there. I have an additional problem with the contact form when not using JavaScript: i must set the required field to all tax details fields to avoid the “(optional)” suffix, and because they _are_ required when that checkbox is enabled, but i can not set them optional when the check is unchecked. My solution for now is to ignore the form validation, and later i will add some JavaScript that adds the validation again, so it will work in all cases.
2023-06-30 19:32:48 +00:00
select bag_eq(
$$ select name, business_name, vatin::text, address, city, province, postal_code, country_code::text from contact join contact_tax_details using (contact_id) $$,
$$ values ('Contact 2.1', 'Contact 2.1 Ltd', 'ES40404040D', 'Fake St., 123', 'City 2.1', 'Province 2.1', '19486', 'ES')
, ('Contact 2.3', 'Contact 2.3 Ltd', 'ES41414141L', 'Another Fake St., 123', 'City 2.2', 'Province 2.2', '17417', 'ES')
$$,
'Should have updated all contacts tax details'
);
select bag_eq(
$$ select name, phone::text from contact join contact_phone using (contact_id) $$,
$$ values ('Contact 2.1', '+34 999 99 99 99')
, ('Contact 2.3', '+34 111111111')
$$,
'Should have updated all contacts phone'
);
select bag_eq(
$$ select name, email::text from contact join contact_email using (contact_id) $$,
$$ values ('Contact 2.1', 'c1@c1')
, ('Contact 2.3', 'd2@d2')
$$,
'Should have updated all contacts email'
);
select bag_eq(
$$ select name, uri::text from contact join contact_web using (contact_id) $$,
$$ values ('Contact 2.1', 'https://c')
, ('Contact 2.3', 'https://d')
$$,
'Should have updated all contacts web'
);
select bag_eq(
$$ select name, iban::text from contact join contact_iban using (contact_id) $$,
$$ values ('Contact 2.1', 'NL68RABO7792285766')
, ('Contact 2.3', 'NL48RABO6482008283')
$$,
'Should have updated all contacts IBAN'
);
select bag_eq(
$$ select name, bic::text from contact join contact_swift using (contact_id) $$,
$$ values ('Contact 2.2', 'ASNBNL21')
, ('Contact 2.3', 'BOFSNL21002')
$$,
'Should have updated all contacts BIC'
);
select *
from finish();
rollback;