-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
163 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { z } from 'zod'; | ||
|
||
import { BadRequestError, Body, Controller, Get, Param, Put, UseMiddlewares } from '@/common/http'; | ||
import { Config } from '@/config'; | ||
import { auth, customerServiceOnly } from '@/middleware'; | ||
|
||
const CONFIG_SCHEMAS: Record<string, z.Schema<any>> = { | ||
weekday: z.array(z.number()), | ||
work_time: z.object({ | ||
from: z.object({ | ||
hours: z.number(), | ||
minutes: z.number(), | ||
seconds: z.number(), | ||
}), | ||
to: z.object({ | ||
hours: z.number(), | ||
minutes: z.number(), | ||
seconds: z.number(), | ||
}), | ||
}), | ||
evaluation: z.object({ | ||
timeLimit: z.number().int().min(0), | ||
}), | ||
}; | ||
|
||
@Controller('config') | ||
@UseMiddlewares(auth, customerServiceOnly) | ||
export class ConfigController { | ||
@Get(':key') | ||
getEvaluation(@Param('key') key: string) { | ||
if (!(key in CONFIG_SCHEMAS)) { | ||
throw new BadRequestError(`Invalid config key "${key}"`); | ||
} | ||
return Config.get(key); | ||
} | ||
|
||
@Put(':key') | ||
async setEvaluation(@Param('key') key: string, @Body() body: any) { | ||
const schema = CONFIG_SCHEMAS[key]; | ||
if (!schema) { | ||
throw new BadRequestError(`Invalid config key "${key}"`); | ||
} | ||
const data = schema.parse(body); | ||
await Config.set(key, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { differenceInMilliseconds } from 'date-fns'; | ||
|
||
import { Ticket } from '@/model/Ticket'; | ||
import { Config } from '@/config'; | ||
|
||
export class TicketService { | ||
async isTicketEvaluable(ticket: Ticket) { | ||
if (!ticket.closedAt) { | ||
return true; | ||
} | ||
const evaluationConfig = (await Config.get('evaluation')) as | ||
| { | ||
timeLimit: number; | ||
} | ||
| undefined; | ||
if (!evaluationConfig || !evaluationConfig.timeLimit) { | ||
return true; | ||
} | ||
return differenceInMilliseconds(new Date(), ticket.closedAt) <= evaluationConfig.timeLimit; | ||
} | ||
} | ||
|
||
export const ticketService = new TicketService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useConfig, useUpdateConfig } from '@/api/config'; | ||
import { LoadingCover } from '@/components/common'; | ||
import { Button, Form, InputNumber } from 'antd'; | ||
|
||
export function EvaluationConfig() { | ||
const { data, isLoading } = useConfig<{ timeLimit: number }>('evaluation'); | ||
|
||
const { mutate, isLoading: isSaving } = useUpdateConfig('evaluation'); | ||
|
||
return ( | ||
<div className="p-10"> | ||
{isLoading && <LoadingCover />} | ||
{!isLoading && ( | ||
<Form | ||
layout="vertical" | ||
initialValues={{ | ||
...data, | ||
timeLimit: Math.floor((data?.timeLimit || 0) / 1000 / 60), | ||
}} | ||
onFinish={(data) => | ||
mutate({ | ||
...data, | ||
timeLimit: data.timeLimit * 1000 * 60, | ||
}) | ||
} | ||
> | ||
<Form.Item | ||
name="timeLimit" | ||
label="评价时限" | ||
extra="工单关闭后多长时间内允许用户评价,设为 0 表示没有限制" | ||
> | ||
<InputNumber min={0} addonAfter="分钟" /> | ||
</Form.Item> | ||
<Button type="primary" htmlType="submit" loading={isSaving}> | ||
保存 | ||
</Button> | ||
</Form> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,43 @@ | ||
import { UseQueryOptions, useQuery, UseMutationOptions, useMutation, useQueryClient } from 'react-query'; | ||
import { | ||
UseQueryOptions, | ||
useQuery, | ||
UseMutationOptions, | ||
useMutation, | ||
useQueryClient, | ||
} from 'react-query'; | ||
import { http } from '@/leancloud'; | ||
|
||
async function fetchConfig<T>(key: string) { | ||
const { data } = await http.get<T>(`/api/2/config/${key}`); | ||
return data | ||
return data; | ||
} | ||
|
||
async function updateConfig<T>(key: string, value: T) { | ||
const { data } = await http.patch<T>(`/api/2/config/${key}`, value) | ||
return data | ||
const { data } = await http.put<T>(`/api/2/config/${key}`, value); | ||
return data; | ||
} | ||
|
||
export const useConfig = <T>(key: string, options?: UseQueryOptions<T, Error>) => useQuery({ | ||
queryKey: ['config', key], | ||
queryFn: () => fetchConfig<T>(key), | ||
...options, | ||
}); | ||
export const useConfig = <T>(key: string, options?: UseQueryOptions<T, Error>) => | ||
useQuery({ | ||
queryKey: ['config', key], | ||
queryFn: () => fetchConfig<T>(key), | ||
...options, | ||
}); | ||
|
||
export const useUpdateConfig = <T>(key: string, options?: UseMutationOptions<T, Error, T>) => { | ||
const queryClient = useQueryClient(); | ||
return useMutation({ | ||
mutationFn: (value) => updateConfig(key, value).then(data => { | ||
queryClient.setQueryData(['config', key], data); | ||
return data | ||
}), | ||
mutationFn: (value) => | ||
updateConfig(key, value).then((data) => { | ||
queryClient.setQueryData(['config', key], data); | ||
return data; | ||
}), | ||
...options, | ||
onSuccess: ((data, ...rest) => { | ||
queryClient.setQueryData(['config', key], data); | ||
onSuccess: (data, vars, ctx) => { | ||
queryClient.setQueryData(['config', key], vars); | ||
if (options?.onSuccess) { | ||
options?.onSuccess(data, ...rest) | ||
options?.onSuccess(data, vars, ctx); | ||
} | ||
}) | ||
}, | ||
}); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters