Skip to content

Commit

Permalink
refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
VijeshVS committed May 29, 2024
1 parent 75c6914 commit ef2d5cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
12 changes: 5 additions & 7 deletions lib/actions/createPublicUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ import { invokeRedis } from "../services/redisPublicGenerate";
import { urlSchema } from "../zod/url";

const createPublicUrl = async (formData: FormData) => {
const longUrl: any = formData.get("long_url");
const longUrl: string = formData.get("long_url") as string;

const errors = urlSchema.safeParse({
const validation = urlSchema.safeParse({
long_url: longUrl,
});

if (!errors.success) {
if (!validation.success) {
return {
shortUrl : "",
status: HTTP_STATUS.BAD_REQUEST
};
}

try {
const res = await invokeRedis(longUrl);
const res:string = await invokeRedis(longUrl);
return {
shortUrl : res,
status : HTTP_STATUS.OK
}
;
} catch (e) {
return {
shortUrl : "",
status : HTTP_STATUS.INTERNAL_SERVER_ERROR
};
}
}
};

Expand Down
43 changes: 35 additions & 8 deletions lib/services/privateLinkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import authOptions from "@/lib/authOptions";
import validateURLCreateReq from "@/lib/validations/url_create";
import PrismaClientManager from "@/lib/services/pgConnect";
import { HTTP_STATUS } from "@/lib/constants";
import { linkType } from "@/interfaces/types";

const alphabetOnlySchema = z.string().regex(/^[a-zA-Z]+$/);


export async function createPrivateLink(formdata : FormData) {

const posgresInstance = PrismaClientManager.getInstance();
const prisma = posgresInstance.getPrismaClient();
const { title,long_url, status } = await validateURLCreateReq(formdata);
const session: ISessionType | null = await getServerSession(authOptions);

let custom_short_code:any = formdata.get('short_code')
let custom_short_code:string = formdata.get('short_code') as string

// if custom short code exists
if(custom_short_code){

// check if duplicate
const link = await prisma.links.findFirst({
const link: linkType | null = await prisma.links.findFirst({
where: {
short_code:custom_short_code
}
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function createPrivateLink(formdata : FormData) {
}
}

const shortIdLength = await incrementCounter();
const shortIdLength:number = await incrementCounter();

if(!custom_short_code)
custom_short_code = base62_encode(shortIdLength);
Expand Down Expand Up @@ -92,9 +92,9 @@ export async function createPrivateLink(formdata : FormData) {
export async function updatePrivateLink(formdata : FormData){
const prisma = PrismaClientManager.getInstance().getPrismaClient();

const title:any = formdata.get('title')
const shortcode:any = formdata.get('short_code')
const linkId:any = formdata.get('linkId')
const title: string = formdata.get('title') as string
const shortcode: string = formdata.get('short_code') as string
const linkId: number = Number.parseInt(formdata.get('linkId') as string)

try{
const link = await prisma.links.findFirst({
Expand All @@ -109,7 +109,7 @@ export async function updatePrivateLink(formdata : FormData){

await prisma.links.update({
where:{
id:Number.parseInt(linkId)
id: linkId
},
data:{
title:title,
Expand All @@ -128,3 +128,30 @@ export async function updatePrivateLink(formdata : FormData){
}
}
}

export async function getTutorialStatus(){
const prisma = PrismaClientManager.getInstance().getPrismaClient();
const session: ISessionType | null = await getServerSession(authOptions);

const TutorialStatus = {
createLink : false,
clickLink : false,
checkAnalytics:false
}

if (!session?.user) {
return {
status: HTTP_STATUS.UNAUTHORIZED
}
}

const link:linkType | null = await prisma.links.findFirst({});

if(link){
TutorialStatus.createLink = true;
}

// If engagement then check
// If checked Analytics thenc check

}

0 comments on commit ef2d5cd

Please sign in to comment.