Skip to content

Commit

Permalink
[API] หน้าสร้าง Page ให้ Validate URL ของเพจด้วย #380
Browse files Browse the repository at this point in the history
  • Loading branch information
junsudas committed Dec 8, 2021
1 parent 62ee540 commit 24c1576
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
18 changes: 18 additions & 0 deletions api-spanboon/src/api/controllers/PageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,15 @@ export class PageController {
return res.status(400).send(errorResponse);
}

// check dupicate uniqueId
if (pages.pageUsername !== undefined && pages.pageUsername !== null && pages.pageUsername !== '') {
const isContainsUniqueId = await this.userService.isContainsUniqueId(pages.pageUsername);
if (isContainsUniqueId !== undefined && isContainsUniqueId) {
const errorResponse = ResponseUtil.getErrorResponse('PageUsername already exists', undefined);
return res.status(400).send(errorResponse);
}
}

const fileName = userObjId + FileUtil.renameFile();
let assetCreate: Asset;

Expand Down Expand Up @@ -1959,6 +1968,15 @@ export class PageController {
return res.status(400).send(ResponseUtil.getSuccessResponse('Invalid Page Id', undefined));
}

// check dupicate uniqueId
if (pages.pageUsername !== undefined && pages.pageUsername !== null && pages.pageUsername !== '') {
const isContainsUniqueId = await this.userService.isContainsUniqueId(pages.pageUsername, undefined, pageUpdate.pageUsername);
if (isContainsUniqueId !== undefined && isContainsUniqueId) {
const errorResponse = ResponseUtil.getErrorResponse('PageUsername already exists', undefined);
return res.status(400).send(errorResponse);
}
}

let pageName = pages.name;
let pagesUsername = pages.pageUsername;
let pageSubTitle = pages.subTitle;
Expand Down
32 changes: 29 additions & 3 deletions api-spanboon/src/api/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,42 @@ export class UserService {
}
}

public isContainsUniqueId(uniqueId: string): Promise<boolean> {
public isContainsUniqueId(uniqueId: string, ignoreUserUniqueId?: string, ignorePageUniqueId?: string): Promise<boolean> {
if (uniqueId === undefined || uniqueId === null || uniqueId === '') {
return Promise.resolve(undefined);
}

return new Promise(async (resolve, reject) => {
try {
const checkUniqueIdUserQuey = { where: { uniqueId } };
let checkUniqueIdUserQuey: any = { where: { uniqueId } };
if (ignoreUserUniqueId !== undefined && ignoreUserUniqueId !== null && ignoreUserUniqueId !== '') {
checkUniqueIdUserQuey = {
$and: [
{
uniqueId
}, {
uniqueId: {
$nin: [ignoreUserUniqueId]
}
}
]
};
}
const checkUniqueIdUser: User = await this.findOne(checkUniqueIdUserQuey);
const checkPageUsernameQuey = { where: { pageUsername: uniqueId } };
let checkPageUsernameQuey: any = { where: { pageUsername: uniqueId } };
if (ignorePageUniqueId !== undefined && ignorePageUniqueId !== null && ignorePageUniqueId !== '') {
checkPageUsernameQuey = {
$and: [
{
pageUsername: uniqueId
}, {
pageUsername: {
$nin: [ignorePageUniqueId]
}
}
]
};
}
const checkPageUsername: any = await this.pageRepository.findOne(checkPageUsernameQuey);

if ((checkUniqueIdUser !== null && checkUniqueIdUser !== undefined) || (checkPageUsername !== null && checkPageUsername !== undefined)) {
Expand Down

0 comments on commit 24c1576

Please sign in to comment.