Skip to content

Commit

Permalink
Merge pull request #12 from fayazara/feat-feedback-details-new-schema…
Browse files Browse the repository at this point in the history
…-api-validation

fixed - feedback details new schema api validation
  • Loading branch information
fayazara authored Sep 2, 2023
2 parents 3c5e983 + 1921245 commit 220fe85
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 44 deletions.
10 changes: 5 additions & 5 deletions lib/types/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export interface Feedback {
id?: number;
projectId: number;
message: string;
origin?: string;
user?: FeedbackUser;
country_name?: string;
device_details?: FeedbackDeviceDetails;
custom_attributes?: Object;
origin?: string | null;
user?: FeedbackUser | string | null;
country_name?: string | null;
device_details?: FeedbackDeviceDetails | string | null;
custom_attributes?: Object | string | null;
createdAt: Date;
internal_notes?: string;
updatedAt: Date;
Expand Down
11 changes: 3 additions & 8 deletions server/api/projects/[id]/[id].patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@ import { Feedback } from "../../../../lib/types/project";
export default eventHandler(async (event) => {
const { getId, getCategory, getStatus } = useValidation(event);
const id = await getId();
const category = await getCategory()
const status = await getStatus()
const category = await getCategory();
const status = await getStatus();

const session = await requireUserSession(event);
const userId = session.user.id;

// const userId = 1

const feedback: Feedback = await updateFeedback(
{
status,
category,
updatedAt: new Date(),
},
and(
eq(tables.feedbacks.id, id),
eq(tables.feedbacks.userId, userId)
)
and(eq(tables.feedbacks.id, id))
);

return feedback;
Expand Down
7 changes: 2 additions & 5 deletions server/api/projects/[id]/feedbacks.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default eventHandler(async (event) => {
// const session = await requireUserSession(event);
// const userId = session.user.id

// const userId = 1

const filterBy: any = and(
eq(tables.feedbacks.projectId, id),
Expand All @@ -23,12 +22,10 @@ export default eventHandler(async (event) => {
const feedbacks: Feedback[] = await getFeedbacks(
{
id: tables.feedbacks.id,
userId: tables.feedbacks.userId,
userEmail: tables.feedbacks.userEmail,
userName: tables.feedbacks.userName,
userData: tables.feedbacks.user,
category: tables.feedbacks.category,
projectId: tables.feedbacks.projectId,
feedback: tables.feedbacks.feedback,
message: tables.feedbacks.message,
status: tables.feedbacks.status,
createdAt: tables.feedbacks.createdAt,
updatedAt: tables.feedbacks.updatedAt,
Expand Down
13 changes: 9 additions & 4 deletions server/api/projects/[id]/feedbacks.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { useHelper } from "../../../utils/helper";
import { useValidation } from "../../../utils/validate";

export default eventHandler(async (event) => {
const body = await readBody(event);
const projectId = getRouterParam(event, "id");
const session = await requireUserSession(event);
const userId = session?.user?.id;

const { getMessage, getCategory, getStatus, getProjectId } = useValidation(event);
const message = await getMessage();
const category = await getCategory();
const status = await getStatus();
const projectId = await getProjectId();

const feedbackInstance: Feedback = await insertFeedback({
...body,
message,
category,
status,
projectId,
createdAt: new Date(),
updatedAt: new Date(),
Expand Down
2 changes: 1 addition & 1 deletion server/db/query/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export const getFeedbacks = async (
export const updateFeedback = async (
data: any,
filterBy: any
): Promise<Feedback> =>
): Promise<any> =>
useDb().update(tables.feedbacks).set(data).where(filterBy).returning().get();
26 changes: 5 additions & 21 deletions server/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,11 @@ export const useValidation = (event: any) => {
return avatar;
};

const getFeedback = async () => {
const { feedback } = await useValidatedBody(event, {
feedback: z.string(),
const getMessage = async () => {
const { message } = await useValidatedBody(event, {
message: z.string(),
});
return feedback;
};

const getUserEmail = async () => {
const { userEmail } = await useValidatedBody(event, {
userEmail: z.string().optional(),
});
return userEmail;
};

const getUserName = async () => {
const { userName } = await useValidatedBody(event, {
userName: z.string().optional(),
});
return userName;
return message;
};

const getCategory = async () => {
Expand Down Expand Up @@ -128,9 +114,7 @@ export const useValidation = (event: any) => {
getStatus,
getWebsite,
getAvatar,
getFeedback,
getUserEmail,
getUserName,
getMessage,
getCategory,
getOrigin,
getProjectId,
Expand Down

0 comments on commit 220fe85

Please sign in to comment.