Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/features/sync/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const MappingTableRow = ({ openConfirmDialog }: { openConfirmDialog: (id?: strin
mapItem.fileChannelValue?.[0],
)}
onChange={(val) => onUserSelectorValueChange(val, index)}
disabled={!!mapItem.status}
disabled={!!mapItem?.id || mapItem.status === null}
options={unselectedChannelList || {}}
/>
</td>
Expand All @@ -93,7 +93,7 @@ const MappingTableRow = ({ openConfirmDialog }: { openConfirmDialog: (id?: strin
onChange={(val) => onDropboxFolderChange(val, index)}
options={tempFolders || []}
placeholder="Search Dropbox folder"
disabled={!!mapItem.status}
disabled={!!mapItem?.id || mapItem.status === null}
/>
</td>
<td className="w-[150px] whitespace-nowrap px-6 py-2 text-gray-500 text-sm">
Expand Down
11 changes: 7 additions & 4 deletions src/features/sync/hooks/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState } from 'react'
import { useAuthContext } from '@/features/auth/hooks/useAuth'
import { useUserChannel } from '@/features/sync/hooks/useUserChannel'
import type { MapList } from '@/features/sync/types'
import { deleteFetcher, postFetcher } from '@/helper/fetcher.helper'
import { customFetcher } from '@/helper/fetcher.helper'
import { getFreshFolders } from '@/helper/table.helper'

import { type UserCompanySelectorInputValue, UserCompanySelectorObject } from '@/lib/copilot/types'
Expand Down Expand Up @@ -76,7 +76,8 @@ export const useTable = () => {
updateTempMapListState(index, { status: null })

try {
await postFetcher(
await customFetcher(
'POST',
`/api/sync?token=${user.token}`,
{},
{
Expand All @@ -99,7 +100,8 @@ export const useTable = () => {
updateTempMapListState(index, { status: !tempMapList[index].status })

try {
await postFetcher(
await customFetcher(
'POST',
`/api/sync/update-status?token=${user.token}`,
{},
{
Expand Down Expand Up @@ -156,7 +158,8 @@ export const useRemoveChannelSync = () => {
}))

try {
await deleteFetcher(
await customFetcher(
'DELETE',
`/api/sync/remove?token=${user.token}`,
{},
{
Expand Down
18 changes: 16 additions & 2 deletions src/features/sync/lib/MapFiles.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { and, asc, eq, isNotNull, isNull, sql } from 'drizzle-orm'
import { and, asc, eq, isNotNull, isNull, or, sql } from 'drizzle-orm'
import httpStatus from 'http-status'
import { ApiError } from 'node_modules/copilot-node-sdk/dist/codegen/api'
import z from 'zod'
Expand All @@ -16,6 +16,7 @@ import {
type FileSyncUpdatePayload,
fileFolderSync,
} from '@/db/schema/fileFolderSync.schema'
import APIError from '@/errors/APIError'
import type {
DropboxFileListFolderResultEntries,
MapList,
Expand Down Expand Up @@ -189,7 +190,11 @@ export class MapFilesService extends AuthenticatedDropboxService {
.where(
and(
eq(channelSync.portalId, this.user.portalId),
eq(channelSync.assemblyChannelId, payload.assemblyChannelId),
or(
eq(channelSync.assemblyChannelId, payload.assemblyChannelId),
eq(channelSync.dbxRootPath, payload.dbxRootPath),
),
isNull(channelSync.deletedAt),
),
)
logger.info('MapFilesService#getOrCreateChannelMap :: Got channel map', channel)
Expand All @@ -200,6 +205,15 @@ export class MapFilesService extends AuthenticatedDropboxService {
.returning()
channel = newChannel[0]
logger.info('MapFilesService#getOrCreateChannelMap :: Created channel map', channel)
} else {
if (
channel.assemblyChannelId !== payload.assemblyChannelId ||
channel.dbxRootPath !== payload.dbxRootPath
)
throw new APIError(
'Mapping already exists for the given file channel or dropbox folder',
httpStatus.BAD_REQUEST,
)
}

return channel
Expand Down
26 changes: 8 additions & 18 deletions src/helper/fetcher.helper.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
export const postFetcher = async (
export const customFetcher = async (
method: 'GET' | 'POST' | 'DELETE' | 'PUT',
url: string,
headers: Record<string, string>,
options: Record<string, string>,
) => {
return await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers,
},
...options,
})
}

export const deleteFetcher = async (
url: string,
headers: Record<string, string>,
options: Record<string, string>,
) => {
return await fetch(url, {
method: 'DELETE',
const response = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
...headers,
},
...options,
})
if (!response.ok) {
throw new Error(`Fetch failed with status code: ${response.status}`)
}
return response
}