Skip to content

Commit

Permalink
Add timestamp to exported snippets
Browse files Browse the repository at this point in the history
Should close: #232
  • Loading branch information
minottic committed Sep 22, 2023
1 parent 0e48ad5 commit 12cff7a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
27 changes: 15 additions & 12 deletions sci-log-db/src/__tests__/unit/service.export-snippet.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ describe('Export service unit', function (this: Suite) {
let textElement: Element;
let exportService: ExportService;
const textcontent = '<img src="" title="123"><img src="" title="456">';
const date = new Date(2023, 3, 13, 11, 13, 15);
const subsnippets = [
new Paragraph({
linkType: LinkType.COMMENT,
textcontent: '<p>a comment</p>',
updatedAt: new Date(2023, 3, 13),
updatedAt: date,
updatedBy: 'test',
}),
new Paragraph({
linkType: LinkType.QUOTE,
textcontent: '<p>a quote</p>',
updatedAt: new Date(2023, 3, 13),
updatedAt: date,
updatedBy: 'test',
}),
new Paragraph({
linkType: LinkType.PARAGRAPH,
textcontent: '<p>a paragraph sub</p>',
updatedAt: new Date(2023, 3, 13),
updatedAt: date,
updatedBy: 'test',
}),
];
Expand All @@ -42,7 +43,7 @@ describe('Export service unit', function (this: Suite) {
{fileHash: '789', accessHash: 'ghi'},
],
tags: ['tag1', 'tag2'],
updatedAt: new Date(2023, 3, 13),
updatedAt: date,
updatedBy: 'test',
linkType: LinkType.PARAGRAPH,
});
Expand Down Expand Up @@ -100,10 +101,10 @@ describe('Export service unit', function (this: Suite) {
textElement,
);
expect(dateAndAuthor.innerHTML).to.be.eql(
`<snippet-header>1 / 13 Apr 2023 / test</snippet-header>${textcontent}`,
`<snippet-header>1 / 13 Apr 2023, 11:13:15 / test</snippet-header>${textcontent}`,
);
expect(dateAndAuthor.outerHTML).to.be.eql(
`<div><snippet-header>1 / 13 Apr 2023 / test</snippet-header>${textcontent}</div>`,
`<div><snippet-header>1 / 13 Apr 2023, 11:13:15 / test</snippet-header>${textcontent}</div>`,
);
});

Expand Down Expand Up @@ -153,20 +154,22 @@ describe('Export service unit', function (this: Suite) {
});

it('addTitle', () => {
sandbox.stub(Date.prototype, 'toLocaleDateString').returns('13 Apr 2023');
sandbox
.stub(Date.prototype, 'toLocaleDateString')
.returns('13 Apr 2023, 11:13:15');
exportService['addTitle']('Some test');
expect(exportService.body.innerHTML).to.be.eql(
'<h1>Some test: 13 Apr 2023</h1><hr style="border-top: 5px solid;">',
'<h1>Some test: 13 Apr 2023, 11:13:15</h1><hr style="border-top: 5px solid;">',
);
});

it('deep', () => {
const deep = exportService['deep'](paragraph);
expect(deep.innerHTML).to.be.eql(
'<snippet-header>1 / 13 Apr 2023 / test</snippet-header><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag>',
'<snippet-header>1 / 13 Apr 2023, 11:13:15 / test</snippet-header><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag>',
);
expect(deep.outerHTML).to.be.eql(
'<imagesnippet><snippet-header>1 / 13 Apr 2023 / test</snippet-header><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag></imagesnippet>',
'<imagesnippet><snippet-header>1 / 13 Apr 2023, 11:13:15 / test</snippet-header><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag></imagesnippet>',
);
});

Expand All @@ -185,8 +188,8 @@ describe('Export service unit', function (this: Suite) {
});

const subsnippetTest = [
'<snippet data-quote="remove-if-last"><imagesnippet><snippet-header>1 / 13 Apr 2023 / test</snippet-header><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag></imagesnippet></snippet><snippetcomment><div><snippet-header>1.1 / 13 Apr 2023 / test</snippet-header><p>a comment</p></div></snippetcomment><snippet><div><snippet-header>1.2 / 13 Apr 2023 / test</snippet-header><p>a paragraph sub</p></div></snippet>',
'<snippetquote><div><snippet-header>1 / 13 Apr 2023 / test</snippet-header><p>a quote</p></div></snippetquote><snippet data-quote="keep"><imagesnippet><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag></imagesnippet></snippet>',
'<snippet data-quote="remove-if-last"><imagesnippet><snippet-header>1 / 13 Apr 2023, 11:13:15 / test</snippet-header><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag></imagesnippet></snippet><snippetcomment><div><snippet-header>1.1 / 13 Apr 2023, 11:13:15 / test</snippet-header><p>a comment</p></div></snippetcomment><snippet><div><snippet-header>1.2 / 13 Apr 2023, 11:13:15 / test</snippet-header><p>a paragraph sub</p></div></snippet>',
'<snippetquote><div><snippet-header>1 / 13 Apr 2023, 11:13:15 / test</snippet-header><p>a quote</p></div></snippetquote><snippet data-quote="keep"><imagesnippet><div><img src="http://localhost:3000/images/abc" title="123"><img src="http://localhost:3000/images/def" title="456"></div><snippet-tag>tag1</snippet-tag><snippet-tag>tag2</snippet-tag></imagesnippet></snippet>',
];
subsnippetTest.forEach((t, i) => {
it(`paragraphToHTML ${i}`, () => {
Expand Down
12 changes: 10 additions & 2 deletions sci-log-db/src/services/export-snippets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {RestBindings, Server} from '@loopback/rest';
import PDFMerger from 'pdf-merger-js';
import Prism from 'prismjs';
import LoadLanguages from 'prismjs/components/';
import {omit} from 'lodash';

@bind({
scope: BindingScope.TRANSIENT,
Expand All @@ -17,7 +18,14 @@ export class ExportService {
body: HTMLBodyElement;
dateOptions = {
locales: 'en-GB',
options: {year: 'numeric', month: 'short', day: 'numeric'} as const,
options: {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
} as const,
};
batchSize = 2000;
paragraphCounter: number;
Expand Down Expand Up @@ -171,7 +179,7 @@ export class ExportService {
titleName ?? 'Scilog'
}: ${new Date().toLocaleDateString(
this.dateOptions.locales,
this.dateOptions.options,
omit(this.dateOptions.options, 'hour', 'minute', 'second'),
)}`;
this.body.append(title);
const hr = this.document.createElement('hr');
Expand Down

0 comments on commit 12cff7a

Please sign in to comment.