Skip to content

Commit

Permalink
Fix storyblok unpublished and deleted webhook (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Jan 8, 2024
1 parent feb73c9 commit e4f856f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 69 deletions.
4 changes: 4 additions & 0 deletions src/webhooks/dto/story.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ export class StoryDto {
@IsOptional()
@IsNumber()
space_id?: number;

@IsOptional()
@IsNumber()
full_slug?: string;
}
2 changes: 1 addition & 1 deletion src/webhooks/webhooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('WebhooksService', () => {
expect.assertions(1);

const body = {
action: STORYBLOK_STORY_STATUS_ENUM.DELETED,
action: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
story_id: mockSession.storyblokId,
text: '',
};
Expand Down
167 changes: 99 additions & 68 deletions src/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import { PartnerAccessEntity } from '../entities/partner-access.entity';
import { PartnerAccessRepository } from '../partner-access/partner-access.repository';
import { SessionRepository } from '../session/session.repository';
import { UserRepository } from '../user/user.repository';
import { CAMPAIGN_TYPE, SIMPLYBOOK_ACTION_ENUM, storyblokToken } from '../utils/constants';
import {
CAMPAIGN_TYPE,
SIMPLYBOOK_ACTION_ENUM,
STORYBLOK_STORY_STATUS_ENUM,
storyblokToken,
} from '../utils/constants';
import { StoryDto } from './dto/story.dto';
import { EmailCampaignRepository } from './email-campaign/email-campaign.repository';
import { TherapySessionRepository } from './therapy-session.repository';
Expand Down Expand Up @@ -457,86 +462,112 @@ export class WebhooksService {
const action = data.action;
const story_id = data.story_id;

let story;
if (action === STORYBLOK_STORY_STATUS_ENUM.PUBLISHED) {
let story;

const Storyblok = new StoryblokClient({
accessToken: storyblokToken,
cache: {
clear: 'auto',
type: 'memory',
},
});
const Storyblok = new StoryblokClient({
accessToken: storyblokToken,
cache: {
clear: 'auto',
type: 'memory',
},
});

try {
const {
data: { story: storyblokData },
} = await Storyblok.get(`cdn/stories/${story_id}`);
story = storyblokData;
} catch (error) {
throw new HttpException(error, HttpStatus.NOT_FOUND);
}
try {
const {
data: { story: storyblokData },
} = await Storyblok.get(`cdn/stories/${story_id}`);
story = storyblokData;
} catch (error) {
throw new HttpException(error, HttpStatus.NOT_FOUND);
}

if (!story) {
throw new HttpException('STORY NOT FOUND', HttpStatus.NOT_FOUND);
}
if (!story) {
throw new HttpException('STORY NOT FOUND', HttpStatus.NOT_FOUND);
}

const storyData = {
name: story.name,
slug: story.full_slug,
status: action,
storyblokId: Number(story.id),
storyblokUuid: story.uuid,
};
try {
if (story.content?.component === 'Course') {
let course = await this.courseRepository.findOne({
storyblokId: story.id,
});
const storyData = {
name: story.name,
slug: story.full_slug,
status: action,
storyblokId: story_id,
storyblokUuid: story.uuid,
};
try {
if (story.content?.component === 'Course') {
let course = await this.courseRepository.findOne({
storyblokId: story_id,
});

if (!!course) {
course.status = action;
course.slug = story.full_slug;
} else {
course = this.courseRepository.create(storyData);
}
course.name = story.content?.name;
course = await this.courseRepository.save(course);
if (!!course) {
course.status = action;
course.slug = story.full_slug;
} else {
course = this.courseRepository.create(storyData);
}
course.name = story.content?.name;
course = await this.courseRepository.save(course);

await this.coursePartnerService.updateCoursePartners(
story.content?.included_for_partners,
course.id,
);
return course;
} else if (
story.content?.component === 'Session' ||
story.content?.component === 'session_iba'
) {
const course = await this.courseRepository.findOne({
storyblokUuid: story.content.course,
});
await this.coursePartnerService.updateCoursePartners(
story.content?.included_for_partners,
course.id,
);
return course;
} else if (
story.content?.component === 'Session' ||
story.content?.component === 'session_iba'
) {
const course = await this.courseRepository.findOne({
storyblokUuid: story.content.course,
});

if (!course.id) {
throw new HttpException('COURSE NOT FOUND', HttpStatus.NOT_FOUND);
}

if (!course.id) {
throw new HttpException('COURSE NOT FOUND', HttpStatus.NOT_FOUND);
const session = await this.sessionRepository.findOne({
storyblokId: story_id,
});

const newSession = !!session
? {
...session,
status: action,
slug: story.full_slug,
name: story.name,
course: course,
courseId: course.id,
}
: this.sessionRepository.create({ ...storyData, ...{ courseId: course.id } });
return await this.sessionRepository.save(newSession);
}
} catch (error) {
throw error;
}
} else if (
action === STORYBLOK_STORY_STATUS_ENUM.UNPUBLISHED ||
action === STORYBLOK_STORY_STATUS_ENUM.DELETED
) {
// Story was unpublished or deleted so cant be fetched to check story type (Course or Session)
// Try to find course with matching story_id first
const course = await this.courseRepository.findOne({
storyblokId: story_id,
});

if (!!course) {
course.status = action;
return await this.courseRepository.save(course);
} else if (!course) {
// No course found, try finding session instead
const session = await this.sessionRepository.findOne({
storyblokId: story.id,
storyblokId: story_id,
});

const newSession = !!session
? {
...session,
status: action,
slug: story.full_slug,
name: story.name,
course: course,
courseId: course.id,
}
: this.sessionRepository.create({ ...storyData, ...{ courseId: course.id } });
return await this.sessionRepository.save(newSession);
if (!!session) {
session.status = action;
return await this.sessionRepository.save(session);
}
}
} catch (error) {
throw error;
}
}

Expand Down

0 comments on commit e4f856f

Please sign in to comment.