Skip to content

Commit

Permalink
Merge pull request #192 from Sunbird-cQube/dev
Browse files Browse the repository at this point in the history
Dev to staging
  • Loading branch information
Thejagd27 authored Apr 21, 2023
2 parents ba20db3 + 1ed38f6 commit 80c6342
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class DataEmissionService {
invalidArray.push(record);
errorCounter = errorCounter + 1;
} else {
validArray.push(this.service.formatDataToCSVBySchema(record, schema));
validArray.push(await this.service.formatDataToCSVBySchema(record, schema));
validCounter = validCounter + 1;
}
ingestionTypeBodyArray = []
Expand Down
29 changes: 27 additions & 2 deletions src/ingestion/services/generic-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ ajv.addKeyword({
}
});

ajv.addKeyword('stripNewline', {
keyword: 'stripNewline',
type: 'string',
validate: (schema, data) => {
if (typeof data === 'string') {
return true;
}
return false;
},
compile: (schema, parentSchema) => {
return (data) => {
if (typeof data === 'string') {
return data.replace(/\n/g, '');
}
return data;
};
},
});


@Injectable()
export class GenericFunction {

Expand Down Expand Up @@ -83,10 +103,11 @@ export class GenericFunction {

async formatDataToCSVBySchema(input: any, schema: InputSchema, addQuotes = true) {
const {properties} = schema;
Object.keys(input).forEach(property => {
Object.keys(input).forEach(async property => {
if (properties[property]) {
if (addQuotes && properties[property].type === 'string') {
input[property] = `'${input[property]}'`;
let inputData = await this.removeNewLine(`${input[property]}`);
input[property] = `'${inputData}'`;
} else if (properties[property].type === 'integer' || properties[property].type === 'number' || properties[property].type === 'float') {
input[property] = Number(input[property]);
}
Expand All @@ -95,6 +116,10 @@ export class GenericFunction {
return input;
}

async removeNewLine(input) {
return input.replace(/\n/g, '').replace('\'','');
}

async getDate() {
let yourDate = new Date();
const formattedDate = yourDate.toLocaleDateString('en-GB', {
Expand Down

0 comments on commit 80c6342

Please sign in to comment.