jws-frontend/src/utils/mrz.ts
Methapon Metanipat 972f6ba13e
feat: menu request list (#75)
* feat: i18n

* feat: request list

* refactor: hide stat transition on app.scss

* feat: request list i18n

* feat: request list => constants and main page

* feat: add store

* feat: add fetch data

* feat: add utilities fn

* feat: add store function / types

* refactor: request list type

* refactor: request list constants

* refactor: quotation card => add customData and badge color props

* feat: avatar group components

* feat: request list group

* refactor: request list => remove tab, add table data

* feat: send search query

* feat: add parameter

* refactor: remove unused function

* fix: rename component lits to list

* feat: show stats from api

* chore: cleanup

* refactor: make it type safe

* refactor: accept rotate flow id as parameter

* feat: use page size component

* feat: add component, data display & expansion product

* feat: i18n

* refactor: constants and request list table

* refactor: type code, createdAt, updatedAt

* refactor: utils function changThemeMode

* feat: request list => view page

* refactor: use type instead of infer from value

* fix: function getEmployeeName att type

* refactor: fetch work list

* refactor: loop work list

* feat: add i18n duty

* feat: add form issue component

* feat: add form issue section

* fix: store error

* refactor: edit by value

* refactor: accept basic info from outside instead

* feat: add status filter support on fetch

* refactor: remove delete button

* refactor: wording

* feat/fix: request list i18n & constant

* feat: document type

* feat/refactor: request list => document expansion

* refactor: doc expansion use FormGroupHead

* refactor: fetch data based on id from route param

* refactor: text area disable

* feat: properties expansion display (mocking)

* refactor: add document at product relation

* refactor: edit get value product

* feat: get workflow step to show on top

* refactor: add type

* refactor: add get attachment

* refactor: add view attachment

* refactor: edit file name

* refactor: define props get hide icon

* refactor: edit align row

* refactor: by value table document

* refactor: by value row table

* feat: add independent ocr dialog

* chore: clean up

* refactor: accept more props and small adjustment

* fix: error withDefault call

* feat: accept default metadata when open

* fix: typo

* feat: add override hook when finish ocr

* feat: reset state on open

* feat: detect reader result is actually string

* fix: variable name conflict

* feat: properties to input component

* feat: properties input in properties expansion

* feat: properties expansion data (temporary)

* refactor: add i18n status work

* refactor: edit type work status and add step status

* refactor: add edit status work

* refactor: edit step work

* refactor: properties data type

* refactor: filter selected product & specific properties

* feat: add emit event

* refactor: change variable name for better understanding

* refactor: hide step that no properties

* refactor: work status type to validate

* feat: work status color

* refactor: key for filename

* refactor: close expansion when change step

* refactor: responsive meta data

* refactor: product expansion responsive

* fix: dark mode step text color

* fix: document expansion table no data label

* refactor: main page body bordered and overflow hidden

* refactor: use utils function instead

* refactor: add process

* refactor: by value  name

* refactor: add upload file

* refactor: upload file

* refactor: by value

* fix: option worker type

* refactor: fetchRequestAttachment after edit

* fix: metadata display

* refactor: add class full-height

* refactor: edit type

* refactor: fetch file

* refactor: by value visa

* refactor: request list attributes type

* fix: properties to input props (placeholder, readonly, disable)

* feat: request list properties function

* fix: error when no workflow

* docs: update comment to fix indent

* refactor: step type (attributes)

* refactor: add attributes payload on editStatusRequestWork function

* feat/refactor: functional form expansion/filter worklist

* refactor: set attributes properties after submit

* refactor: add request work ready status

* feat: request list => form employee component

* feat/refactor: form expansion select user/layout

* fix: properties readonly

---------

Co-authored-by: puriphatt <puriphat@frappet.com>
Co-authored-by: Thanaphon Frappet <thanaphon@frappet.com>
2024-11-22 18:02:03 +07:00

233 lines
6.7 KiB
TypeScript

import moment from 'moment';
type MRZ = {
type: 'TD1' | 'TD2' | 'TD3';
zone: string[];
};
type Field = {
field: string;
format?: (value: string, obj?: Record<string, string>) => string;
/**
* Post process value after format.
* Useful for extract one field to multiple fields .
*
* @example
* Convert fullname into firstname and lastname
*
* ```ts
* (value) => [
* { field: 'first_name', value: value.split(' ').at(0) || '' },
* { field: 'last_name', value: value.split(' ').slice(1).join(' ') },
* ]
* ```
*/
process?: (value: string) => { field: string; value: string }[];
};
type FieldList = Record<string, Field | Field[]>;
const DEFAULT_FIELD = {
documentType: { field: 'doc_type' },
documentNo: { field: 'doc_number' },
documentNoCheck: { field: 'doc_number_check' },
name: {
field: 'full_name',
format: (value, _) => value.replace(/0/, 'O'),
process: (value) => [
{ field: 'first_name', value: value.split(' ').at(0) || '' },
{ field: 'last_name', value: value.split(' ').slice(1).join(' ') },
],
},
country: {
field: 'country',
format: (value, _) => value.replace(/0/, 'O'),
},
nationality: {
field: 'nationality',
format: (value, _) => value.replace(/0/, 'O'),
},
gender: {
field: 'sex',
format: (value, _) => (value === 'M' ? 'male' : 'female'),
},
birthDate: {
field: 'birth_date',
format: (value, _) => moment(value, 'YYMMDD').format('YYYY-MM-DD'),
},
birthDateCheck: { field: 'birth_date_check' },
expireDate: {
field: 'expire_date',
format: (value, _) => moment(value, 'YYMMDD').format('YYYY-MM-DD'),
},
expireDateCheck: { field: 'expire_date_check' },
optionalData: { field: 'optional_data' },
optionalDataCheck: { field: 'optional_data_check' },
lineCheck: { field: 'line_check' },
} satisfies FieldList;
const MRZ_TD_1 = [
new RegExp(
[
`(?<${DEFAULT_FIELD.documentType.field}>[0-9A-Z<]{2})`,
`(?<${DEFAULT_FIELD.country.field}>[0-9A-Z<]{3})`,
`(?<${DEFAULT_FIELD.documentNo.field}>[0-9A-Z<]{9})`,
`(?<${DEFAULT_FIELD.documentNoCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.optionalData.field}>[0-9A-Z<]{15})`,
].join(''),
),
new RegExp(
[
`(?<${DEFAULT_FIELD.birthDate.field}>[0-9A-Z<]{6})`,
`(?<${DEFAULT_FIELD.birthDateCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.gender.field}>[mfMF<]{1})`,
`(?<${DEFAULT_FIELD.expireDate.field}>[0-9A-Z<]{6})`,
`(?<${DEFAULT_FIELD.expireDateCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.nationality.field}>[0-9A-Z<]{3})`,
`(?<${DEFAULT_FIELD.optionalData.field}>[A-Z0-9<]{11})`,
`(?<${DEFAULT_FIELD.lineCheck.field}>[0-9A-Z<]{1})`,
].join(''),
),
new RegExp([`(?<${DEFAULT_FIELD.name.field}>[A-Z<]{30})`].join('')),
];
const MRZ_TD_2 = [
new RegExp(
[
`(?<${DEFAULT_FIELD.documentType.field}>[0-9A-Z<]{2})`,
`(?<${DEFAULT_FIELD.country.field}>[0-9A-Z<]{3})`,
`(?<${DEFAULT_FIELD.name.field}>[A-Z<]{31})`,
].join(''),
),
new RegExp(
[
`(?<${DEFAULT_FIELD.documentNo.field}>[0-9A-Z<]{9})`,
`(?<${DEFAULT_FIELD.documentNoCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.nationality.field}>[0-9A-Z<]{3})`,
`(?<${DEFAULT_FIELD.birthDate.field}>[0-9A-Z<]{6})`,
`(?<${DEFAULT_FIELD.birthDateCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.gender.field}>[mfMF]{1})`,
`(?<${DEFAULT_FIELD.expireDate.field}>[0-9A-Z<]{6})`,
`(?<${DEFAULT_FIELD.expireDateCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.optionalData.field}>[A-Z0-9<]{7})`,
`(?<${DEFAULT_FIELD.lineCheck.field}>[0-9A-Z<]{1})`,
].join(''),
),
];
const MRZ_TD_3 = [
new RegExp(
[
`(?<${DEFAULT_FIELD.documentType.field}>[A-Z0-9<]{2})`,
`(?<${DEFAULT_FIELD.country.field}>[0-9A-Z<]{3})`,
`(?<${DEFAULT_FIELD.name.field}>[A-Z0-9<]{39})`,
].join(''),
),
new RegExp(
[
`(?<${DEFAULT_FIELD.documentNo.field}>[0-9A-Z<]{9})`,
`(?<${DEFAULT_FIELD.documentNoCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.nationality.field}>[0-9A-Z<]{3})`,
`(?<${DEFAULT_FIELD.birthDate.field}>[0-9A-Z<]{6})`,
`(?<${DEFAULT_FIELD.birthDateCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.gender.field}>[mfMF<]{1})`,
`(?<${DEFAULT_FIELD.expireDate.field}>[0-9A-Z<]{6})`,
`(?<${DEFAULT_FIELD.expireDateCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.optionalData.field}>[A-Z0-9<]{14})`,
`(?<${DEFAULT_FIELD.optionalDataCheck.field}>[0-9A-Z<]{1})`,
`(?<${DEFAULT_FIELD.lineCheck.field}>[0-9A-Z<]{1})`,
].join(''),
),
];
function mrzCleanResult(obj: Record<string, string>) {
Object.entries(obj).forEach(([k, v]) => {
obj[k] = v
.replace(/</g, ' ')
.replace(/\s{2,}/, ' ')
.trim();
});
const original = structuredClone(obj);
for (const value of Object.values(DEFAULT_FIELD)) {
if (obj[value.field]) {
if ('format' in value) {
obj[value.field] = value.format(obj[value.field], original);
}
if ('process' in value) {
value.process(obj[value.field]).forEach((result) => {
obj[result.field] = result.value;
});
delete obj[value.field];
continue;
}
}
}
return obj;
}
function mrzFieldExtract(expression: RegExp, line: string) {
if (expression.test(line)) {
return expression.exec(line)?.groups || {};
}
return {};
}
export function checkSum(data: string) {
if (!/[0-9A-Z<]/.test(data)) return null;
const sum = data.split('').reduce((a, v, i) => {
if (v === '<') return a;
const num = Number(v);
const weight = [7, 3, 1][i % 3];
if (Number.isNaN(num)) {
return a + (v.charCodeAt(0) - 55) * weight;
}
return a + num * weight;
}, 0);
return sum % 10;
}
export function parseType1(mrz: MRZ) {
const result: Record<string, string> = {};
mrz.zone.forEach((line, i) =>
Object.assign(result, mrzFieldExtract(MRZ_TD_1[i], line)),
);
return { mrz, result: mrzCleanResult(result) };
}
export function parseType2(mrz: MRZ) {
const result: Record<string, string> = {};
mrz.zone.forEach((line, i) =>
Object.assign(result, mrzFieldExtract(MRZ_TD_2[i], line)),
);
return { mrz, result: mrzCleanResult(result) };
}
export function parseType3(mrz: MRZ) {
const result: Record<string, string> = {};
mrz.zone.forEach((line, i) =>
Object.assign(result, mrzFieldExtract(MRZ_TD_3[i], line)),
);
return { mrz, result: mrzCleanResult(result) };
}
export function parseMRZ(mrz: MRZ) {
if (mrz.type === 'TD1') return parseType1(mrz);
if (mrz.type === 'TD2') return parseType2(mrz);
if (mrz.type === 'TD3') return parseType3(mrz);
return null;
}