Skip to content

Commit

Permalink
♻️ refactor webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Vondee committed Aug 9, 2024
1 parent 4b49782 commit 2592d26
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions packages/server/src/controllers/index.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { IStandardResponse, SendApiResponse } from '@utils/api.response';
import { HttpException } from '@exceptions/HttpException';
import { LivepeerEvent } from '@interfaces/livepeer.interface';
import { ClippingStatus } from '@interfaces/session.interface';
import { StateStatus, StateType } from '@interfaces/state.interface';
import SessionService from '@services/session.service';
import StageService from '@services/stage.service';
import StateService from '@services/state.service';
import { type IStandardResponse, SendApiResponse } from '@utils/api.response';
import { updateEventVideoById } from '@utils/firebase';
import { getAsset, getDownloadUrl } from '@utils/livepeer';
import StorageService from '@utils/s3';
import { validateWebhook } from '@utils/validateWebhook';
import {
Body,
Controller,
FormField,
Get,
Header,
Post,
Route,
Security,
Tags,
Body,
Post,
Header,
UploadedFile,
FormField,
Security,
} from 'tsoa';
import startAITools from '@aitools/main';
import { validateWebhook } from '@utils/validateWebhook';
import StageService from '@services/stage.service';
import { LivepeerEvent } from '@interfaces/livepeer.interface';
import SessionService from '@services/session.service';
import { getAsset, getDownloadUrl } from '@utils/livepeer';
import StateService from '@services/state.service';
import { StateStatus } from '@interfaces/state.interface';
import StorageService from '@utils/s3';
import { HttpException } from '@exceptions/HttpException';
import { updateEventVideoById } from '@utils/firebase';

@Tags('Index')
@Route('')
Expand Down Expand Up @@ -96,18 +96,12 @@ export class IndexController extends Controller {

private async assetReady(id: string) {
const asset = await getAsset(id);
const session = await this.sessionService.findOne({
assetId: asset.id,
});

if (!session) {
return SendApiResponse('No session found', null, '400');
}
// const ipfs = await uploadToIpfs(id);
const session = await this.sessionService.findOne({ assetId: asset.id });
if (!session) throw new HttpException(404, 'No session found');
await this.sessionService.update(session._id.toString(), {
// ipfsURI: ipfs,
videoUrl: asset.playbackUrl,
playbackId: asset.playbackId,
clippingStatus: ClippingStatus.completed,
} as any);

if (session.firebaseId && asset.playbackUrl) {
Expand All @@ -116,19 +110,14 @@ export class IndexController extends Controller {
mp4Url: await getDownloadUrl(asset.id),
});
}

const state = await this.stateService.getAll({
sessionId: session._id.toString(),
const state = await this.stateService.findOne({
_id: session._id.toString(),
type: StateType.video
});

if (!state || state.length === 0) {
return SendApiResponse('No state found', null, '400');
}
await this.stateService.update(state[0]._id.toString(), {
if (!state) throw new HttpException(404, 'No state found');
await this.stateService.update(state._id.toString(), {
status: StateStatus.completed,
});

// await startAITools(payload.payload.id);
}

private async assetFailed(id: string) {
Expand All @@ -137,20 +126,20 @@ export class IndexController extends Controller {
assetId: asset.id,
});

if (!session) {
throw 'No session found';
}
if (!session) throw new HttpException(404, 'No session found');

const state = await this.stateService.getAll({
const state = await this.stateService.findOne({
sessionId: session._id.toString(),
type: StateType.video,
});
if (!state) throw new HttpException(404, 'No state found');

if (!state || state.length === 0) {
throw 'No state found';
}
await this.sessionService.update(session._id.toString(), {
clippingStatus: ClippingStatus.failed,
} as any);

await this.stateService.update(state[0]._id.toString(), {
status: StateStatus.error,
await this.stateService.update(state._id.toString(), {
status: StateStatus.failed,
});
}
}

0 comments on commit 2592d26

Please sign in to comment.