Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[O2B-1186] Specify Id when creating QC flag type #1500

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { checkboxes } from '../../../components/Filters/common/filters/checkboxF
*/
export const qcFlagTypesActiveColumns = {
id: {
visible: false,
primary: true,
name: 'Id',
visible: true,
sortable: true,
},
name: {
name: 'Name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
*/
_initOrResetData() {
this.formData = {
id: null,
name: '',
method: '',
color: null,
Expand All @@ -70,6 +71,6 @@
* @inheritDoc
*/
isValid() {
return this.formData.name.length > 0 && this.formData.method.length > 0;
return this.formData.id && this.formData.name.length > 0 && this.formData.method.length > 0;

Check warning on line 74 in lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationModel.js

View check run for this annotation

Codecov / codecov/patch

lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationModel.js#L74

Added line #L74 was not covered by tests
}
}
12 changes: 11 additions & 1 deletion lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@
* @return {Component} Return the view of the inputs
*/
const qcFlagTypeCreationComponent = (qcFlagTypeCreationModel) => {
const { name, method, color, bad } = qcFlagTypeCreationModel.formData;
const { id, name, method, color, bad } = qcFlagTypeCreationModel.formData;

Check warning on line 27 in lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationPage.js

View check run for this annotation

Codecov / codecov/patch

lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationPage.js#L27

Added line #L27 was not covered by tests

const panel = [
h(PanelComponent, [
h(LabelPanelHeaderComponent, { for: 'id' }, 'Id'),
h('input#id.form-control.mb2', {
placeholder: 'Enter the QC Flag Type id ...',
value: id,
min: 1,
type: 'number',
oninput: (e) => qcFlagTypeCreationModel.patchFormData({ id: e.target.value }),

Check warning on line 37 in lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationPage.js

View check run for this annotation

Codecov / codecov/patch

lib/public/views/QcFlagTypes/Create/QcFlagTypeCreationPage.js#L37

Added line #L37 was not covered by tests
}),
]),
h(PanelComponent, [
h(LabelPanelHeaderComponent, { for: 'name' }, 'Name'),
h('input#name.form-control.mb2', {
Expand Down
1 change: 1 addition & 0 deletions lib/server/controllers/qcFlagTypes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const getQcFlagTypeByIdHandler = async (req, res) => {
const createQCFlagTypeHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
DtoFactory.bodyOnly({
id: Joi.number().required(),
name: Joi.string().required(),
method: Joi.string().required(),
color: Joi.string().regex(/#[0-9a-fA-F]{6}/).optional().allow(null),
Expand Down
6 changes: 4 additions & 2 deletions lib/server/services/qualityControlFlag/QcFlagTypeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class QcFlagTypeService {
*/
async create(parameters, relations = {}) {
const {
id,
name,
method,
bad,
Expand All @@ -79,13 +80,14 @@ class QcFlagTypeService {
const user = await getUserOrFail({ userId, externalUserId });

const existingQcFlagType = await QcFlagTypeRepository.findOne({
where: { [Op.or]: [{ name }, { method }] },
where: { [Op.or]: [{ name }, { method }, { id }] },
});
if (existingQcFlagType) {
throw new ConflictError(`A QC flag with name ${name} or ${method} already exists`);
throw new ConflictError(`A QC flag type with id ${id} or name ${name} or method ${method} already exists`);
}

const newFlagInstance = await QcFlagTypeRepository.insert({
id,
name,
method,
bad,
Expand Down
12 changes: 8 additions & 4 deletions test/api/qcFlagTypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ module.exports = () => {
describe('POST /api/qcFlagTypes', () => {
it('should successfuly create QC Flag Type', async () => {
const parameters = {
id: 1000,
name: 'A',
method: 'AA+',
bad: false,
Expand All @@ -312,18 +313,19 @@ module.exports = () => {
expect(response.status).to.be.equal(201);
const { data: newQCFlag } = response.body;
{
const { name, method, bad, color } = newQCFlag;
expect({ name, method, bad, color }).to.be.eql(parameters);
const { id, name, method, bad, color } = newQCFlag;
expect({ id, name, method, bad, color }).to.be.eql(parameters);
}
{
const fetchedQcFlagType = await qcFlagTypeService.getById(newQCFlag.id);
const { name, method, bad, color } = fetchedQcFlagType;
expect({ name, method, bad, color }).to.be.eql(parameters);
const { id, name, method, bad, color } = fetchedQcFlagType;
expect({ id, name, method, bad, color }).to.be.eql(parameters);
}
});

it('should fail when no name is provided', async () => {
const parameters = {
id: 1000,
method: 'AA+',
bad: false,
color: '#FFAA00',
Expand All @@ -338,6 +340,7 @@ module.exports = () => {

it('should fail when no method is provided', async () => {
const parameters = {
id: 1000,
name: 'A',
bad: false,
color: '#FFAA00',
Expand All @@ -352,6 +355,7 @@ module.exports = () => {

it('should fail when no bad info is provided', async () => {
const parameters = {
id: 1000,
name: 'A',
method: 'A++',
color: '#FFAA00',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ module.exports = () => {
describe('Creating QC Flag Type', () => {
it('should successfuly create QC Flag Type', async () => {
const parameters = {
id: 1000,
name: 'A',
method: 'AA+',
bad: false,
Expand All @@ -289,30 +290,33 @@ module.exports = () => {
const relations = { user: { externalUserId: 1 } };
const newQCFlag = await qcFlagTypeService.create(parameters, relations);
{
const { name, method, bad, color, createdBy: { externalId: externalUserId } } = newQCFlag;
expect({ name, method, bad, color, externalUserId }).to.be.eql({ ...parameters, ...relations.user });
const { id, name, method, bad, color, createdBy: { externalId: externalUserId } } = newQCFlag;
expect({ id, name, method, bad, color, externalUserId }).to.be.eql({ ...parameters, ...relations.user });
}
{
const fetchedQCFlag = await qcFlagTypeService.getById(newQCFlag.id);
const { name, method, bad, color, createdBy: { externalId: externalUserId } } = fetchedQCFlag;
expect({ name, method, bad, color, externalUserId }).to.be.eql({ ...parameters, ...relations.user });
const { id, name, method, bad, color, createdBy: { externalId: externalUserId } } = fetchedQCFlag;
expect({ id, name, method, bad, color, externalUserId }).to.be.eql({ ...parameters, ...relations.user });
}
});

it('should fail when QC Flag type with provided name already exists', async () => {
const parameters = {
id: 1000,
name: 'BadPID',
method: 'Bad PID',
bad: false,
};
await assert.rejects(
() => qcFlagTypeService.create(parameters, { user: { externalUserId: 1 } }),
new ConflictError(`A QC flag with name ${parameters.name} or ${parameters.method} already exists`),
// eslint-disable-next-line max-len
new ConflictError(`A QC flag type with id ${parameters.id} or name ${parameters.name} or method ${parameters.method} already exists`),
);
});

it('should fail when no user info is provided', async () => {
const parameters = {
id: 1000,
name: 'A',
method: 'AA+',
bad: false,
Expand Down
5 changes: 4 additions & 1 deletion test/public/qcFlagTypes/creation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,23 @@ module.exports = () => {
await goToPage(page, 'qc-flag-type-creation');
await validateElement(page, 'button#submit[disabled]');

await fillInput(page, 'input#id', '1001');
await fillInput(page, 'input#name', 'LimitedAcceptance');
await fillInput(page, 'input#method', 'Limited acceptance');
await pressElement(page, 'button#submit');
await expectInnerText(
page,
'.alert.alert-danger',
'The request conflicts with existing data: A QC flag with name LimitedAcceptance or Limited acceptance already exists',
// eslint-disable-next-line max-len
'The request conflicts with existing data: A QC flag type with id 1001 or name LimitedAcceptance or method Limited acceptance already exists',
);
});

it('should succesfully create QC Flag Type', async () => {
await goToPage(page, 'qc-flag-type-creation');
await validateElement(page, 'button#submit[disabled]');

await fillInput(page, 'input#id', '1001');
await fillInput(page, 'input#name', 'AAA+');
await fillInput(page, 'input#method', 'A+A+A');
await fillInput(page, 'input[type=color]', '#F000F0');
Expand Down
1 change: 1 addition & 0 deletions test/public/qcFlagTypes/overview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = () => {
await goToPage(page, 'qc-flag-types-overview');

const tableDataValidators = {
id: (id) => !isNaN(id),
name: (name) => name !== '-',
method: (method) => method !== '-',
bad: (isBad) => isBad === 'Yes' || isBad === 'No',
Expand Down
Loading