Skip to content
This repository has been archived by the owner on Aug 8, 2020. It is now read-only.

Add search option functionality for the most #152

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 6 additions & 6 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Config {
log.warn(`**SECURITY ISSUE** Value \`secretJWT\` is less then 16 characters long! We recommend to use a key that has length of 16 at minimum.`)
this.config.serverPort = userConfig.serverPort;
} else {
this.config.serverPort = userConfig.serverPort;
this.config.secretJWT = userConfig.secretJWT;
}

// Check mongodb.url
Expand All @@ -74,18 +74,18 @@ export class Config {
} else if(typeof(userConfig.mongodb.url) != "string") {
log.warn(`Value \`mongodb.url\` is not a string! Using default: ${this.config.mongodb.url}`)
} else if (!userConfig.mongodb.url.startsWith("mongodb://")) {
log.warn(`Value \`mongodb.url\` has a invaild protocol. It has to start with "mongodb://". Using default: ${this.config.mongodb}`)
log.warn(`Value \`mongodb.url\` is a invaild protocol. It has to start with "mongodb://". Using default: ${this.config.mongodb.url}`)
} else {
this.config.serverPort = userConfig.serverPort;
this.config.mongodb.url = userConfig.mongodb.url;
}

// Check mongodb.eraseOnStartup
if(userConfig.mongodb.eraseOnStartup == undefined) {
log.warn(`Value \`mongodb.url\` is undefined! Using default: ${this.config.mongodb.eraseOnStartup}`)
log.warn(`Value \`mongodb.eraseOnStartup\` is undefined! Using default: ${this.config.mongodb.eraseOnStartup}`)
} else if(typeof(userConfig.mongodb.eraseOnStartup) != "boolean") {
log.warn(`Value \`mongodb.url\` is not a boolean! Using default: ${this.config.mongodb.eraseOnStartup}`)
log.warn(`Value \`mongodb.eraseOnStartup\` is not a boolean! Using default: ${this.config.mongodb.eraseOnStartup}`)
} else {
this.config.serverPort = userConfig.serverPort;
this.config.mongodb.eraseOnStartup = userConfig.mongodb.eraseOnStartup;
}
}
}
12 changes: 10 additions & 2 deletions backend/src/endpoints/getAllTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ export default async function getAllTags(req: ModifiedRequest, res: any) {
// Get the document-count for that tag
for(let i = 0; i < user.tags_R.length; i++) {
const currentTag = user.tags_R[i] as ITag;
const docCount: number = await Document.countDocuments({tags_R: {$elemMatch: {$eq: mongoose.Types.ObjectId(currentTag._id)}}});
const docCount: number = await Document.countDocuments({
tags_R: {
$elemMatch: {$eq: mongoose.Types.ObjectId(currentTag._id)
}
},
user_R: {
$eq: mongoose.Types.ObjectId(user._id)
}
});
const tagCount: TagCount = new TagCount(currentTag, docCount);
newTagObj.push(tagCount);
}
Expand All @@ -23,7 +31,7 @@ export default async function getAllTags(req: ModifiedRequest, res: any) {
class TagCount {
_id: mongoose.Types.ObjectId;
name: string;
count: number;
count: number; // This class because of that extra value :D
style: {
colorBackground?: string;
colorForeground?: string;
Expand Down
41 changes: 24 additions & 17 deletions backend/src/endpoints/searchDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ export default async function searchDocuments(req: ModifiedRequest, res: Respons
*/
const userId = req.userID;
const user = await User.findOne({ _id: userId });

if(user == null) {
log.warn(`User with ID ${userId} does not exist in the database`);
res.status(500).send();
return;
}
let query = Document.where("user_R").equals(userId);

console.log("Got request:", req.headers)

// take
const limit = parseInt(req.header("option-limit"), 10);
if(isNaN(limit) == false) {
Expand Down Expand Up @@ -77,17 +80,13 @@ export default async function searchDocuments(req: ModifiedRequest, res: Respons
// title
let title = req.header("option-where-title");
if(title != null) {
title = title.replace(/([.+?=^!:${}()|[\]\/\\])/g, '\\$1');
title = title.replace(/\*{2,}|\*/g, ".*");
query.where("title").regex(new RegExp(`${title}`, 'i')); // Ignoring upper and lower case
query.where("title").regex(ignoreUpperLowerCaseRegex(title));
}

// note
let note = req.header("option-where-note");
if(note != null) {
note = note.replace(/([.+?=^!:${}()|[\]\/\\])/g, '\\$1');
note = note.replace(/\*{2,}|\*/g, ".*");
query.where("note").regex(new RegExp(`${note}`, 'i')); // Ignoring upper and lower case
query.where("note").regex(ignoreUpperLowerCaseRegex(note));
}

// mimeType
Expand All @@ -100,30 +99,32 @@ export default async function searchDocuments(req: ModifiedRequest, res: Respons
const textRecognitionEnabled = req.header("option-where-textRecognition-enabled");
if(textRecognitionEnabled == "true") {
query.where("textRecognition.enabled").equals(true);
} else if(textRecognitionEnabled == "false") {
query.where("textRecognition.enabled").equals(false);
}

// textRecognition-finished
const textRecognitionFinished = req.header("option-where-textRecognition-finished");
const textRecognitionFinished = req.header("option-where-textrecognition-finished");
if(textRecognitionFinished == "true") {
query.where("textRecognition.finished").equals(true);
} else if(textRecognitionFinished == "false") {
query.where("textRecognition.finished").equals(false);
}

// textRecognition-content
const textRecognitionContent = req.header("option-where-textRecognition-content");
const textRecognitionContent = req.header("option-where-textrecognition-content");
if(textRecognitionContent != null) {
query.where("textRecognition.content").regex(new RegExp(`${textRecognitionContent}`));
query.where("textRecognition.content").regex(ignoreUpperLowerCaseRegex(textRecognitionContent));
}

// createdAt range
const createdFrom = parseDateFromHeader(req, "option-where-created-from");
const createdTo = parseDateFromHeader(req, "option-where-created-to");
if(createdFrom != null && createdTo != null) {
query.where("createdAt").gte(createdFrom).lte(createdTo);
let createdFrom = parseDateFromHeader(req, "option-where-created-from");
let createdTo = parseDateFromHeader(req, "option-where-created-to");
if(createdFrom == null) {
createdFrom = new Date(0, 0, 0);
}
if(createdTo == null) {
createdTo = new Date();
}
query.where("createdAt").gte(createdFrom).lte(createdTo);

// updatedAt range
const updatedFrom = parseDateFromHeader(req, "option-where-updated-from");
Expand All @@ -137,7 +138,7 @@ export default async function searchDocuments(req: ModifiedRequest, res: Respons
if(tags != null) {
let parsedTags: Array<string>;
try {
parsedTags = JSON.parse(tags);
parsedTags = tags.split(",");
let objectIds = parsedTags.map((entry) => {
return mongoose.Types.ObjectId(entry);
});
Expand All @@ -157,7 +158,7 @@ export default async function searchDocuments(req: ModifiedRequest, res: Respons
console.log("queryOptions=" + JSON.stringify(query.getOptions(), null, 4));
console.log("################################################################");
console.log("result=" + JSON.stringify(result, null, 4));*/

res.status(200).send(result);
}

Expand All @@ -180,4 +181,10 @@ function parseDateFromHeader(req: Request, fieldName: string): Date|null {
let dayInt = parseInt(day);
let date = new Date(yearInt, monthInt, dayInt);
return date;
}

function ignoreUpperLowerCaseRegex(toRegex: string): RegExp {
toRegex = toRegex.replace(/([.+?=^!:${}()|[\]\/\\])/g, '\\$1');
toRegex = toRegex.replace(/\*{2,}|\*/g, ".*");
return new RegExp(`${toRegex}`, 'i'); // Ignoring upper and lower case
}
67 changes: 53 additions & 14 deletions backend/src/insertDummyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { connection } from "mongoose";
import { generateFilePath } from "./lib/documentOperations";
import { getNextPrimaryNumber } from "./lib/userUtils";
import { log } from "./lib/logging";
import { IDocument } from "./models/document/document.interface";

export async function insertDummyData() {

Expand Down Expand Up @@ -81,7 +82,12 @@ export async function insertDummyData() {
note: "What the title says",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagMisc],
tags_R: [tagMisc],
textRecognition: {
content: "DocSort",
enabled: true,
finished: false
},
marked: true,
createdAt: new Date(2018, 4, 26)
},
Expand All @@ -90,15 +96,25 @@ export async function insertDummyData() {
note: "This is cool text",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagWarning, tagInvoice],
tags_R: [tagWarning, tagInvoice],
textRecognition: {
content: "This text is actually not in the document.",
enabled: true,
finished: true
},
createdAt: new Date(2018, 4, 27)
},
{
title: "Sample document 3",
note: "$4.500 :(",
fileExtension: 'png',
user_R: user_test,
tags: [tagTravel, tagInvoice],
tags_R: [tagTravel, tagInvoice],
textRecognition: {
content: "Wait, why are we doing this?",
enabled: true,
finished: false
},
marked: true,
createdAt: new Date(2018, 4, 27)
},
Expand All @@ -107,72 +123,85 @@ export async function insertDummyData() {
note: "Awwww",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagMisc],
tags_R: [tagMisc],
textRecognition: {
content: "=3*x!$ Wrong OCR",
enabled: true,
finished: false
},
createdAt: new Date(2018, 5, 10)
},
{
title: "Sample document 5",
note: "Did you????",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagInvoice, tagWarning],
tags_R: [tagInvoice, tagWarning],
textRecognition: {
content: "You have to pay $300k to me in Bitcoin!",
enabled: true,
finished: true
},
createdAt: new Date(2018, 5, 30)
},
{
title: "Sample document 6 - Magic",
note: "Right?",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagMisc, tagWarning],
tags_R: [tagMisc, tagWarning],
createdAt: new Date(2018, 7, 6)
},
{
title: "Sample document 7",
note: "More magic",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagInvoice, tagMisc, tagTravel, tagWarning],
tags_R: [tagInvoice, tagMisc, tagTravel, tagWarning],
createdAt: new Date(2018, 7, 14)
},
{
title: "Can you OCR?",
note: "[Fill in here a good note]",
fileExtension: 'pdf',
user_R: user_brother,
tags: [tagInvoice],
tags_R: [tagInvoice],
createdAt: new Date(2018, 9, 11)
},
{
title: "Sample document 9 - Base64",
note: "Bipp, boop, bipp",
fileExtension: 'pdf',
user_R: user_brother,
tags: [tagMisc, tagWarning],
tags_R: [tagMisc, tagWarning],
createdAt: new Date(2018, 9, 31)
},
{
title: "Sample document 10",
note: "Funny joke",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagTravel],
tags_R: [tagTravel],
createdAt: new Date(2018, 12, 3)
},
{
title: "Sample document 11",
note: "ftw!!!!!!",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagInvoice],
tags_R: [tagInvoice],
createdAt: new Date(2019, 1, 12)
},
{
title: "Sample document 12 - pnnnnnnnng",
note: "PDF is boring, now PNG.",
fileExtension: 'pdf',
user_R: user_test,
tags: [tagMisc, tagInvoice],
createdAt: new Date(2019, 2, 17)
tags_R: [tagMisc, tagInvoice],
createdAt: new Date(2019, 2, 17),
number: {
secondary: 1
}
},
{
title: "Sample document 13",
Expand Down Expand Up @@ -204,6 +233,11 @@ export async function insertDummyData() {
fileExtension: 'png',
user_R: user_brother,
tags_R: [tagWarning, tagTUI],
textRecognition: {
content: "Cards Against Humanity costs $25 but it's the same in euro. Why?",
enabled: true,
finished: true
},
createdAt: new Date(2019, 2, 14)
},
{
Expand All @@ -212,6 +246,11 @@ export async function insertDummyData() {
fileExtension: 'jpg',
user_R: user_brother,
tags_R: [tagWaitForReponse, tagTUI],
textRecognition: {
content: "Pizza is like sex. If it's good, it's really good. If it's bad it's still good.",
enabled: true,
finished: false
},
marked: true,
createdAt: new Date(2019, 2, 16)
}
Expand All @@ -220,7 +259,7 @@ export async function insertDummyData() {
log.info("Saving dummy documents...")
for(let i = 0; i < documents.length; i++) {
const document = documents[i];
document.number = {};
if(document.number == undefined) document.number = {};
document.number.primary = await getNextPrimaryNumber(document.user_R._id);
const db = await Document.create(document);

Expand Down
Loading