53 lines
2.4 KiB
TypeScript
53 lines
2.4 KiB
TypeScript
import { Context } from "@/context";
|
|
import { buildUserProfile, UserProfile } from "./type";
|
|
import { db } from "@/storage/db";
|
|
import { RelationshipStatus } from "@prisma/client";
|
|
import { relationshipSet } from "./relationshipSet";
|
|
import { relationshipGet } from "./relationshipGet";
|
|
|
|
export async function friendRemove(ctx: Context, uid: string): Promise<UserProfile | null> {
|
|
return await db.$transaction(async (tx) => {
|
|
|
|
// Read current user objects
|
|
const currentUser = await tx.account.findUnique({
|
|
where: { id: ctx.uid },
|
|
include: { githubUser: true }
|
|
});
|
|
const targetUser = await tx.account.findUnique({
|
|
where: { id: uid },
|
|
include: { githubUser: true }
|
|
});
|
|
if (!currentUser || !targetUser) {
|
|
return null;
|
|
}
|
|
|
|
// Read relationship status
|
|
const currentUserRelationship = await relationshipGet(tx, currentUser.id, targetUser.id);
|
|
const targetUserRelationship = await relationshipGet(tx, targetUser.id, currentUser.id);
|
|
|
|
// If status is requested, set it to rejected
|
|
if (currentUserRelationship === RelationshipStatus.requested) {
|
|
await relationshipSet(tx, currentUser.id, targetUser.id, RelationshipStatus.rejected);
|
|
return buildUserProfile(targetUser, RelationshipStatus.rejected);
|
|
}
|
|
|
|
// If they are friends, change it to pending and requested
|
|
if (currentUserRelationship === RelationshipStatus.friend) {
|
|
await relationshipSet(tx, targetUser.id, currentUser.id, RelationshipStatus.requested);
|
|
await relationshipSet(tx, currentUser.id, targetUser.id, RelationshipStatus.pending);
|
|
return buildUserProfile(targetUser, RelationshipStatus.requested);
|
|
}
|
|
|
|
// If status is pending, set it to none
|
|
if (currentUserRelationship === RelationshipStatus.pending) {
|
|
await relationshipSet(tx, currentUser.id, targetUser.id, RelationshipStatus.none);
|
|
if (targetUserRelationship !== RelationshipStatus.rejected) {
|
|
await relationshipSet(tx, targetUser.id, currentUser.id, RelationshipStatus.none);
|
|
}
|
|
return buildUserProfile(targetUser, RelationshipStatus.none);
|
|
}
|
|
|
|
// Return the target user profile with status none
|
|
return buildUserProfile(targetUser, currentUserRelationship);
|
|
});
|
|
} |