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
66 changes: 17 additions & 49 deletions apps/mail/actions/connections.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,26 @@
"use server";
'use server';

import { connection, user } from "@zero/db/schema";
import { headers } from "next/headers";
import { and, eq } from "drizzle-orm";
import { type IConnection } from "@/types";
import { auth } from "@/lib/auth";
import { db } from "@zero/db";

export async function getConnections() {
try {
const headersList = await headers();
const session = await auth.api.getSession({ headers: headersList });

if (!session) {
throw new Error("Unauthorized, reconnect");
}

const userId = session?.user?.id;

if (!userId) {
throw new Error("Unauthorized, reconnect");
}

const connections = (await db
.select({
id: connection.id,
email: connection.email,
name: connection.name,
picture: connection.picture,
createdAt: connection.createdAt,
})
.from(connection)
.where(eq(connection.userId, userId))) as IConnection[];

return connections;
} catch (error) {
console.error("Failed to fetch connections:", error);
throw new Error("Failed to fetch connections");
}
}
import { getAuthenticatedUserId } from '@/app/api/utils';
import { connection, user } from '@zero/db/schema';
import { type IConnection } from '@/types';
import { headers } from 'next/headers';
import { and, eq } from 'drizzle-orm';
import { auth } from '@/lib/auth';
import { db } from '@zero/db';

export async function deleteConnection(connectionId: string) {
try {
const headersList = await headers();
const session = await auth.api.getSession({ headers: headersList });

if (!session) {
throw new Error("Unauthorized, reconnect");
throw new Error('Unauthorized, reconnect');
}

const userId = session?.user?.id;

if (!userId) {
throw new Error("Unauthorized, reconnect");
throw new Error('Unauthorized, reconnect');
}

await db
Expand All @@ -67,8 +35,8 @@ export async function deleteConnection(connectionId: string) {

return { success: true };
} catch (error) {
console.error("Failed to delete connection:", error);
throw new Error("Failed to delete connection");
console.error('Failed to delete connection:', error);
throw new Error('Failed to delete connection');
}
}

Expand All @@ -78,13 +46,13 @@ export async function putConnection(connectionId: string) {
const session = await auth.api.getSession({ headers: headersList });

if (!session) {
throw new Error("Unauthorized, reconnect");
throw new Error('Unauthorized, reconnect');
}

const userId = session?.user?.id;

if (!userId) {
throw new Error("Unauthorized, reconnect");
throw new Error('Unauthorized, reconnect');
}

const [foundConnection] = await db
Expand All @@ -94,7 +62,7 @@ export async function putConnection(connectionId: string) {
.limit(1);

if (!foundConnection) {
throw new Error("Connection not found");
throw new Error('Connection not found');
}

await db
Expand All @@ -106,7 +74,7 @@ export async function putConnection(connectionId: string) {

return { success: true };
} catch (error) {
console.error("Failed to update connection:", error);
throw new Error("Failed to update connection");
console.error('Failed to update connection:', error);
throw new Error('Failed to update connection');
}
}
13 changes: 7 additions & 6 deletions apps/mail/actions/mail.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use server';
import { deleteActiveConnection, FatalErrors, getActiveDriver } from './utils';
import { IGetThreadResponse } from '@/app/api/driver/types';
import { ParsedMessage } from '@/types';

export const getMails = async ({
Expand Down Expand Up @@ -29,7 +30,7 @@ export const getMails = async ({
}
};

export const getMail = async ({ id }: { id: string }): Promise<ParsedMessage[]> => {
export const getMail = async ({ id }: { id: string }): Promise<IGetThreadResponse> => {
if (!id) {
throw new Error('Missing required fields');
}
Expand Down Expand Up @@ -128,17 +129,17 @@ export const toggleStar = async ({ ids }: { ids: string[] }) => {
return { success: false, error: 'No thread IDs provided' };
}

const threadResults = await Promise.allSettled(
threadIds.map(id => driver.get(id))
);
const threadResults = await Promise.allSettled(threadIds.map((id) => driver.get(id)));

let anyStarred = false;
let processedThreads = 0;

for (const result of threadResults) {
if (result.status === 'fulfilled' && result.value && result.value.length > 0) {
if (result.status === 'fulfilled' && result.value && result.value.messages.length > 0) {
processedThreads++;
const isThreadStarred = result.value.some((message: ParsedMessage) => message.tags?.includes('STARRED'));
const isThreadStarred = result.value.messages.some((message: ParsedMessage) =>
message.tags?.includes('STARRED'),
);
if (isThreadStarred) {
anyStarred = true;
break;
Expand Down
24 changes: 13 additions & 11 deletions apps/mail/actions/notes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use server';

import { notes } from '@/app/api/notes';
import type { Note } from '@/app/api/notes/types';
import { notes } from '@/app/api/notes';

export type ActionResult<T> = {
success: boolean;
Expand All @@ -17,7 +17,7 @@ export async function fetchThreadNotes(threadId: string): Promise<ActionResult<N
console.error('Error fetching thread notes:', error);
return {
success: false,
error: error.message || 'Failed to fetch thread notes'
error: error.message || 'Failed to fetch thread notes',
};
}
}
Expand All @@ -26,7 +26,7 @@ export async function createNote({
threadId,
content,
color = 'default',
isPinned = false
isPinned = false,
}: {
threadId: string;
content: string;
Expand All @@ -40,14 +40,14 @@ export async function createNote({
console.error('Error creating note:', error);
return {
success: false,
error: error.message || 'Failed to create note'
error: error.message || 'Failed to create note',
};
}
}

export async function updateNote(
noteId: string,
data: Partial<Omit<Note, 'id' | 'userId' | 'threadId' | 'createdAt' | 'updatedAt'>>
data: Partial<Omit<Note, 'id' | 'userId' | 'threadId' | 'createdAt' | 'updatedAt'>>,
): Promise<ActionResult<Note>> {
try {
const result = await notes.updateNote(noteId, data);
Expand All @@ -56,7 +56,7 @@ export async function updateNote(
console.error('Error updating note:', error);
return {
success: false,
error: error.message || 'Failed to update note'
error: error.message || 'Failed to update note',
};
}
}
Expand All @@ -69,30 +69,32 @@ export async function deleteNote(noteId: string): Promise<ActionResult<boolean>>
console.error('Error deleting note:', error);
return {
success: false,
error: error.message || 'Failed to delete note'
error: error.message || 'Failed to delete note',
};
}
}

export async function reorderNotes(
notesArray: { id: string; order: number; isPinned?: boolean | null }[]
notesArray: { id: string; order: number; isPinned?: boolean | null }[],
): Promise<ActionResult<boolean>> {
try {
if (!notesArray || notesArray.length === 0) {
console.warn('Attempted to reorder an empty array of notes');
return { success: true, data: true };
}

console.log(`Reordering ${notesArray.length} notes:`,
notesArray.map(({id, order, isPinned}) => ({id, order, isPinned})));
console.log(
`Reordering ${notesArray.length} notes:`,
notesArray.map(({ id, order, isPinned }) => ({ id, order, isPinned })),
);

const result = await notes.reorderNotes(notesArray);
return { success: true, data: result };
} catch (error: any) {
console.error('Error reordering notes:', error);
return {
success: false,
error: error.message || 'Failed to reorder notes'
error: error.message || 'Failed to reorder notes',
};
}
}
Loading