diff --git a/api-spanboon/src/api/controllers/FacebookWebhookController.ts b/api-spanboon/src/api/controllers/FacebookWebhookController.ts index 6da16462a..ab95535a0 100644 --- a/api-spanboon/src/api/controllers/FacebookWebhookController.ts +++ b/api-spanboon/src/api/controllers/FacebookWebhookController.ts @@ -17,19 +17,22 @@ import { PostsService } from '../services/PostsService'; import { SocialPostService } from '../services/SocialPostService'; import { AssetService } from '../services/AssetService'; import { PostsGalleryService } from '../services/PostsGalleryService'; +import { PageConfigService } from '../services/PageConfigService'; import { FacebookWebhookLogs } from '../models/FacebookWebhookLogs'; import { Posts } from '../models/Posts'; import { PostsGallery } from '../models/PostsGallery'; import { SocialPost } from '../models/SocialPost'; +import { PageConfig } from '../models/PageConfig'; import { POST_TYPE } from '../../constants/PostType'; import { ASSET_PATH } from '../../constants/AssetScope'; +import { PAGE_CONFIGS } from '../../constants/PageConfigs'; import { facebook_setup } from '../../env'; @JsonController('/fb_webhook') export class FacebookWebhookController { constructor(private pageSocialAccountService: PageSocialAccountService, private facebookWebhookLogsService: FacebookWebhookLogsService, private pageService: PageService, private postsService: PostsService, private socialPostService: SocialPostService, - private assetService: AssetService, private postsGalleryService: PostsGalleryService) { } + private assetService: AssetService, private postsGalleryService: PostsGalleryService, private pageConfigService: PageConfigService) { } /** * @api {get} /api/fb_webhook/page_feeds WebHook for page feed @@ -98,6 +101,12 @@ export class FacebookWebhookController { continue; } + const isFetchPage = await this.isFetchPage(pageSocialAccount.page); + if (!isFetchPage) { + createLog = false; + continue; + } + // check if fbPostId was post by page const hasSocialPosted = await this.socialPostService.findOne({ pageId: pageSocialAccount.page, socialId: fbPostId, socialType: PROVIDER.FACEBOOK }); if (hasSocialPosted !== undefined) { @@ -205,4 +214,34 @@ export class FacebookWebhookController { return post; } + + private async isFetchPage(pageId: ObjectID): Promise { + if (pageId === undefined) { + return PAGE_CONFIGS.DEFAULT_PAGE_SOCIAL_FACEBOOK_FETCHPOST; + } + + const config = await this.pageConfigService.getConfig(PAGE_CONFIGS.PAGE_SOCIAL_FACEBOOK_FETCHPOST, pageId); + if (config !== undefined && config.value !== undefined) { + if (typeof config.value === 'string') { + const valueString = config.value.toUpperCase(); + + return (valueString === 'TRUE') ? true : false; + } else if (typeof config.value === 'boolean') { + return config.value; + } + + return config.value; + } else { + // auto create page config + const pageConfig = new PageConfig(); + pageConfig.page = pageId; + pageConfig.name = PAGE_CONFIGS.PAGE_SOCIAL_FACEBOOK_FETCHPOST; + pageConfig.type = 'boolean'; + pageConfig.value = PAGE_CONFIGS.DEFAULT_PAGE_SOCIAL_FACEBOOK_FETCHPOST ? 'TRUE' : 'FALSE'; + + await this.pageConfigService.create(pageConfig); + } + + return PAGE_CONFIGS.DEFAULT_PAGE_SOCIAL_FACEBOOK_FETCHPOST; + } } \ No newline at end of file diff --git a/api-spanboon/src/api/controllers/GuestController.ts b/api-spanboon/src/api/controllers/GuestController.ts index 57155d9e2..c4d3383d4 100644 --- a/api-spanboon/src/api/controllers/GuestController.ts +++ b/api-spanboon/src/api/controllers/GuestController.ts @@ -100,7 +100,7 @@ export class GuestController { const data: User = await this.userService.findOne({ where: { username: registerEmail } }); if (data) { - const errorResponse = ResponseUtil.getErrorResponse('This Email already exists', data); + const errorResponse = ResponseUtil.getErrorResponse('This Email already exists', undefined); return res.status(400).send(errorResponse); } else { if (registerPassword === null || registerPassword === undefined) { @@ -138,6 +138,18 @@ export class GuestController { user.customGender = customGender; } + // check uniqueId + if (user.uniqueId === '') { + user.uniqueId = null; + } + if (user.uniqueId !== undefined && user.uniqueId !== null) { + const isContainsUniqueId = await this.userService.isContainsUniqueId(user.uniqueId); + if (isContainsUniqueId !== undefined && isContainsUniqueId) { + const errorResponse = ResponseUtil.getErrorResponse('UniqueId already exists', undefined); + return res.status(400).send(errorResponse); + } + } + let result = await this.userService.create(user); if (result) { @@ -279,6 +291,18 @@ export class GuestController { user.customGender = customGender; } + // check uniqueId + if (user.uniqueId === '') { + user.uniqueId = null; + } + if (user.uniqueId !== undefined && user.uniqueId !== null) { + const isContainsUniqueId = await this.userService.isContainsUniqueId(user.uniqueId); + if (isContainsUniqueId !== undefined && isContainsUniqueId) { + const errorResponse = ResponseUtil.getErrorResponse('UniqueId already exists', users); + return res.status(400).send(errorResponse); + } + } + const resultData: User = await this.userService.create(user); if (resultData) { const userId = resultData.id; @@ -419,6 +443,18 @@ export class GuestController { user.customGender = customGender; } + // check uniqueId + if (user.uniqueId === '') { + user.uniqueId = null; + } + if (user.uniqueId !== undefined && user.uniqueId !== null) { + const isContainsUniqueId = await this.userService.isContainsUniqueId(user.uniqueId); + if (isContainsUniqueId !== undefined && isContainsUniqueId) { + const errorResponse = ResponseUtil.getErrorResponse('UniqueId already exists', users); + return res.status(400).send(errorResponse); + } + } + const resultData: User = await this.userService.create(user); if (resultData) { const userId = resultData.id; diff --git a/api-spanboon/src/api/services/PageConfigService.ts b/api-spanboon/src/api/services/PageConfigService.ts index 0c107d1fa..d2d596208 100644 --- a/api-spanboon/src/api/services/PageConfigService.ts +++ b/api-spanboon/src/api/services/PageConfigService.ts @@ -6,6 +6,7 @@ */ import { Service } from 'typedi'; +import { ObjectID } from 'typeorm'; import { OrmRepository } from 'typeorm-typedi-extensions'; import { SearchUtil } from '../../utils/SearchUtil'; import { PageConfigRepository } from '../repositories/PageConfigRepository'; @@ -34,8 +35,8 @@ export class PageConfigService { return await this.pageConfigRepository.updateOne(query, newValue); } - public async getConfig(name: string): Promise { - const condition = { name }; + public async getConfig(name: string, pageId: ObjectID): Promise { + const condition = { name, page: pageId }; return await this.pageConfigRepository.findOne(condition); } diff --git a/api-spanboon/src/api/services/UserService.ts b/api-spanboon/src/api/services/UserService.ts index 927caf13e..56d904479 100755 --- a/api-spanboon/src/api/services/UserService.ts +++ b/api-spanboon/src/api/services/UserService.ts @@ -9,13 +9,14 @@ import { Service } from 'typedi'; import { OrmRepository } from 'typeorm-typedi-extensions'; import { User } from '../models/User'; import { UserRepository } from '../repositories/UserRepository'; +import { PageRepository } from '../repositories/PageRepository'; import { SearchUtil } from '../../utils/SearchUtil'; import { S3Service } from '../services/S3Service'; @Service() export class UserService { - constructor(@OrmRepository() private userLoginRepository: UserRepository, private s3Service: S3Service) { } + constructor(@OrmRepository() private userLoginRepository: UserRepository, @OrmRepository() private pageRepository: PageRepository, private s3Service: S3Service) { } // find user public find(findCondition?: any): Promise { @@ -78,6 +79,29 @@ export class UserService { } } + public isContainsUniqueId(uniqueId: string): Promise { + if (uniqueId === undefined || uniqueId === null || uniqueId === '') { + return Promise.resolve(undefined); + } + + return new Promise(async (resolve, reject) => { + try { + const checkUniqueIdUserQuey = { where: { uniqueId } }; + const checkUniqueIdUser: User = await this.findOne(checkUniqueIdUserQuey); + const checkPageUsernameQuey = { where: { pageUsername: uniqueId } }; + const checkPageUsername: any = await this.pageRepository.findOne(checkPageUsernameQuey); + + if ((checkUniqueIdUser !== null && checkUniqueIdUser !== undefined) || (checkPageUsername !== null && checkPageUsername !== undefined)) { + resolve(true); + } else { + resolve(false); + } + } catch (error) { + reject(error); + } + }); + } + public cleanUserField(user: any): any { if (user !== undefined && user !== null) { if (user !== undefined && user !== null) { diff --git a/api-spanboon/src/constants/PageConfigs.ts b/api-spanboon/src/constants/PageConfigs.ts index 44809c359..fd0617834 100644 --- a/api-spanboon/src/constants/PageConfigs.ts +++ b/api-spanboon/src/constants/PageConfigs.ts @@ -8,7 +8,9 @@ export const PAGE_CONFIGS = { PAGE_SOCIAL_TWITTER_AUTOPOST: 'page.social.twitter.autopost', PAGE_SOCIAL_FACEBOOK_AUTOPOST: 'page.social.facebook.autopost', + PAGE_SOCIAL_FACEBOOK_FETCHPOST: 'page.social.facebook.fetchpost', // default DEFAULT_PAGE_SOCIAL_TWITTER_AUTOPOST: true, - DEFAULT_PAGE_SOCIAL_FACEBOOK_AUTOPOST: true + DEFAULT_PAGE_SOCIAL_FACEBOOK_AUTOPOST: true, + DEFAULT_PAGE_SOCIAL_FACEBOOK_FETCHPOST: false }; \ No newline at end of file