Skip to content

Commit

Permalink
add tree info queries & setters for all properties
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWang05 committed Oct 18, 2024
1 parent bea151a commit 0282481
Showing 1 changed file with 201 additions and 0 deletions.
201 changes: 201 additions & 0 deletions src/supabase/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,204 @@ export async function removeMultipleTrees(treeIds: string[]) {
throw new Error(`Error removing multiple trees: ${error.message}`);
}
}

/**
* Function to retrieve tree info by UUID
* Returns properties in JSON form: {"bank": null, "date": null, "health_status": null, ...}
*/
export async function getTreeInfo(treeId: string) {
const { data, error } = await supabase.rpc('get_tree_by_uuid', {
tree_uuid: treeId,
});

if (error) {
throw new Error(`Error retrieving tree info: ${error.message}`);
}

return data;
}

// Functions to update each property

// Update species
export async function updateTreeSpecies(treeId: string, newSpecies: string) {
const { error } = await supabase
.from('trees')
.update({ species: newSpecies })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating species: ${error.message}`);
}
}

// Update street address
export async function updateTreeStreetAddress(
treeId: string,
newAddress: string,
) {
const { error } = await supabase
.from('trees')
.update({ street_address: newAddress })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating street address: ${error.message}`);
}
}

// Update bank
export async function updateTreeBank(treeId: string, newBank: number) {
const { error } = await supabase
.from('trees')
.update({ bank: newBank })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating bank: ${error.message}`);
}
}

// Update row
export async function updateTreeRow(treeId: string, newRow: number) {
const { error } = await supabase
.from('trees')
.update({ row: newRow })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating row: ${error.message}`);
}
}

// Update health status
export async function updateTreeHealthStatus(
treeId: string,
newHealthStatus: string,
) {
const { error } = await supabase
.from('trees')
.update({ health_status: newHealthStatus })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating health status: ${error.message}`);
}
}

// Update planted status
export async function updateTreePlanted(treeId: string, isPlanted: boolean) {
const { error } = await supabase
.from('trees')
.update({ planted: isPlanted })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating planted status: ${error.message}`);
}
}

// Update sold status
export async function updateTreeSold(treeId: string, isSold: boolean) {
const { error } = await supabase
.from('trees')
.update({ sold: isSold })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating sold status: ${error.message}`);
}
}

// Update reserved status
export async function updateTreeReserved(treeId: string, isReserved: boolean) {
const { error } = await supabase
.from('trees')
.update({ reserved: isReserved })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating reserved status: ${error.message}`);
}
}

// Update reserved for
export async function updateTreeReservedFor(
treeId: string,
reservedFor?: string,
) {
const { error } = await supabase
.from('trees')
.update({ reserved_for: reservedFor })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating reserved for: ${error.message}`);
}
}

// Update street ready status
export async function updateTreeStreetReady(
treeId: string,
isStreetReady?: boolean,
) {
const { error } = await supabase
.from('trees')
.update({ street_ready: isStreetReady })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating street ready status: ${error.message}`);
}
}

// Update required action
export async function updateTreeRequiredAction(
treeId: string,
requiredAction?: string,
) {
const { error } = await supabase
.from('trees')
.update({ required_action: requiredAction })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating required action: ${error.message}`);
}
}

// Update source
export async function updateTreeSource(treeId: string, source?: string) {
const { error } = await supabase
.from('trees')
.update({ source })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating source: ${error.message}`);
}
}

// Update date
export async function updateTreeDate(treeId: string, date?: Date) {
const { error } = await supabase
.from('trees')
.update({ date })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating date: ${error.message}`);
}
}

// Update QR code URL
export async function updateTreeQrCodeUrl(treeId: string, qrCodeUrl?: string) {
const { error } = await supabase
.from('trees')
.update({ qr_code_url: qrCodeUrl })
.eq('tree_id', treeId);

if (error) {
throw new Error(`Error updating QR code URL: ${error.message}`);
}
}

0 comments on commit 0282481

Please sign in to comment.