Skip to content

Commit

Permalink
refactor: Update file permission function and create message with upd…
Browse files Browse the repository at this point in the history
…ated request
  • Loading branch information
xuelink committed Jun 26, 2024
1 parent 1ebd411 commit 4af13db
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions src/app/store/effects/message.effect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { Injectable } from '@angular/core';
import { createEffect, ofType, Actions } from '@ngrx/effects';
import { HttpErrorResponse } from '@angular/common/http';
import { Observable, catchError, map, mergeMap, of, switchMap } from 'rxjs';
import {
Observable,
catchError,
from,
map,
mergeMap,
of,
switchMap,
} from 'rxjs';

// Interface Imports
import { Message } from 'src/app/models/Message';
import { BucketFile } from 'src/app/models/BucketFile';
import { ErrorInterface } from 'src/app/models/types/errors/error.interface';
import { AxiosError } from 'axios';
import { MessageService } from 'src/app/services/chat/message.service';
import { listMessagesResponseInterface } from 'src/app/models/types/responses/listMessagesResponse.interface';

// Service Imports
import { ApiService } from 'src/app/services/api/api.service';
import { MessageService } from 'src/app/services/chat/message.service';

// Selector and Action Imports
import {
createMessageAction,
Expand Down Expand Up @@ -90,22 +101,45 @@ export class MessageEffects {

return uploadObservable.pipe(
switchMap((uploadResponse) => {
if (messageType === 'image') {
updatedRequest = { ...request, imageId: uploadResponse.$id };
} else if (messageType === 'audio') {
updatedRequest = { ...request, audioId: uploadResponse.$id };
}
// Trigger bucket permission function here
return from(
this.api.function.createExecution(
'update-file-permission',
JSON.stringify({
fileId: uploadResponse.$id,
to: request.to,
type: messageType,
})
)
).pipe(
switchMap(() => {
if (messageType === 'image') {
updatedRequest = {
...request,
imageId: uploadResponse.$id,
};
} else if (messageType === 'audio') {
updatedRequest = {
...request,
audioId: uploadResponse.$id,
};
}

return this.messageService.createMessage(updatedRequest).pipe(
map((payload: Message) =>
createMessageSuccessAction({ payload })
),
catchError((errorResponse: AxiosError) => {
const error: ErrorInterface = {
message: errorResponse?.response?.data['message'],
};
return of(
createMessageFailureAction({ error, payload: updatedRequest })
return this.messageService.createMessage(updatedRequest).pipe(
map((payload: Message) =>
createMessageSuccessAction({ payload })
),
catchError((errorResponse: AxiosError) => {
const error: ErrorInterface = {
message: errorResponse?.response?.data['message'],
};
return of(
createMessageFailureAction({
error,
payload: updatedRequest,
})
);
})
);
})
);
Expand Down Expand Up @@ -173,6 +207,7 @@ export class MessageEffects {

constructor(
private actions$: Actions,
private api: ApiService,
private messageService: MessageService
) {}
}

0 comments on commit 4af13db

Please sign in to comment.