Skip to content

Commit

Permalink
auto need confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Apr 19, 2024
1 parent 84a20ff commit aca0c37
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
31 changes: 19 additions & 12 deletions src/features/need/need.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { TicketService } from '../ticket/ticket.service';
import {
checkNeed,
GRACE_PERIOD,
SIMILAR_NAME_LIMIT,
SIMILAR_NAME_LIMIT_PRODUCT,
SIMILAR_NAME_LIMIT_SERVICE,
validateNeed,
} from 'src/utils/needConfirm';
import { ValidatedDupType } from 'src/types/interfaces/Need';
Expand Down Expand Up @@ -271,17 +272,14 @@ export class NeedController {
const ngoIds = await this.ngoService
.getFlaskNgos()
.then((r) => r.filter((n) => n.isActive).map((n) => n.id));
const notConfirmed = await this.needService.getNotConfirmedNeeds(
null,
swIds,
ngoIds,
);

let toBeConfirmed = config().dataCache.fetchToBeConfirmed();
if (
!toBeConfirmed ||
!toBeConfirmed[0] ||
toBeConfirmed.length !== notConfirmed[1]
) {
if (!toBeConfirmed || !toBeConfirmed[0]) {
const notConfirmed = await this.needService.getNotConfirmedNeeds(
null,
swIds,
ngoIds,
);
for await (const need of notConfirmed[0]) {
console.log(`Mass Confirm preparation: ${need.id}`);
// 1- sync & validate need
Expand Down Expand Up @@ -371,7 +369,16 @@ export class NeedController {
const filtered = similarNameNeeds.filter(
(n) => n.category === need.category,
);
if (filtered.length < SIMILAR_NAME_LIMIT) {
if (
need.type === NeedTypeEnum.PRODUCT &&
filtered.length <= SIMILAR_NAME_LIMIT_PRODUCT
) {
errorMsg = `Similar count error, only ${filtered.length}.`;
}
if (
need.type === NeedTypeEnum.SERVICE &&
filtered.length <= SIMILAR_NAME_LIMIT_SERVICE
) {
errorMsg = `Similar count error, only ${filtered.length}.`;
}

Expand Down
47 changes: 31 additions & 16 deletions src/utils/needConfirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const BASE_AGE_OF_DUPLICATE_3 = 30;

const SIMILAR_URL_PERCENTAGE = 57; // percentage
const SIMILAR_TXT_PERCENTAGE = 15; // percentage
export const SIMILAR_NAME_LIMIT = 20;
export const SIMILAR_NAME_LIMIT_PRODUCT = 20;
export const SIMILAR_NAME_LIMIT_SERVICE = 10;
export const GRACE_PERIOD = 15; // days left after ticket to fix the problem mentioned in ticket
export async function validateNeed(
nestNeed: NeedEntity,
Expand Down Expand Up @@ -332,23 +333,34 @@ export function checkNeed(need: Need, duplicate: Need) {
T = false;
msg = 'Types are different';
}
let titleResult: number;
// compare only first 10 chars
const titleResult =
duplicate.title &&
Number(
getSimilarityPercentage(
title.length > 15 ? title.substring(0, 15) : title,
duplicate.title.length > 15
? duplicate.title.substring(0, 15)
: duplicate.title,
),
);
if (duplicate.title && titleResult > SIMILAR_TXT_PERCENTAGE) {
TT = true;
} else {
TT = false;
msg = 'title are not that similar';
if (type === NeedTypeEnum.PRODUCT) {
titleResult =
duplicate.title &&
Number(
getSimilarityPercentage(
title.length > 15 ? title.substring(0, 15) : title,
duplicate.title.length > 15
? duplicate.title.substring(0, 15)
: duplicate.title,
),
);
if (duplicate.title && titleResult > SIMILAR_TXT_PERCENTAGE) {
TT = true;
} else {
TT = false;
msg = 'title are not that similar';
}
} else if (type === NeedTypeEnum.SERVICE) {
if (!duplicate.title || duplicate.title.length < 1) {
TT = true;
} else {
TT = false;
msg = 'service should not have link';
}
}

// check icons similarity
if (
icon &&
Expand All @@ -372,12 +384,15 @@ export function checkNeed(need: Need, duplicate: Need) {
msg = 'Price error';
}
if (
type === NeedTypeEnum.PRODUCT &&
retailerLink &&
retailerLink.length > 10 &&
duplicate.link &&
duplicate.link.length > 10
) {
R = true;
} else if (type === NeedTypeEnum.SERVICE) {
R = true;
} else {
R = false;
msg = 'retailerLink error';
Expand Down

0 comments on commit aca0c37

Please sign in to comment.