Skip to content

Commit

Permalink
Merge pull request #379 from kaogeek/dev-shiorin
Browse files Browse the repository at this point in the history
 Register ด้วย UniqueId ซ้ำได้ #378
  • Loading branch information
chaluckabs authored Dec 8, 2021
2 parents 567c107 + 62ee540 commit bcee666
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
41 changes: 40 additions & 1 deletion api-spanboon/src/api/controllers/FacebookWebhookController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -205,4 +214,34 @@ export class FacebookWebhookController {

return post;
}

private async isFetchPage(pageId: ObjectID): Promise<boolean> {
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;
}
}
38 changes: 37 additions & 1 deletion api-spanboon/src/api/controllers/GuestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions api-spanboon/src/api/services/PageConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -34,8 +35,8 @@ export class PageConfigService {
return await this.pageConfigRepository.updateOne(query, newValue);
}

public async getConfig(name: string): Promise<any> {
const condition = { name };
public async getConfig(name: string, pageId: ObjectID): Promise<any> {
const condition = { name, page: pageId };
return await this.pageConfigRepository.findOne(condition);
}

Expand Down
26 changes: 25 additions & 1 deletion api-spanboon/src/api/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User[]> {
Expand Down Expand Up @@ -78,6 +79,29 @@ export class UserService {
}
}

public isContainsUniqueId(uniqueId: string): Promise<boolean> {
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) {
Expand Down
4 changes: 3 additions & 1 deletion api-spanboon/src/constants/PageConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

0 comments on commit bcee666

Please sign in to comment.