Skip to content

Commit

Permalink
fix: update deletion method
Browse files Browse the repository at this point in the history
  • Loading branch information
Labham-Jain committed Oct 6, 2023
1 parent cab3285 commit fe9898b
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 187 deletions.
Binary file modified dump.rdb
Binary file not shown.
11 changes: 4 additions & 7 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import express, { Router } from "express"
import { Router } from "express"
import {
getConfigFile,
} from "medusa-core-utils";
import { ConfigModule } from "@medusajs/medusa";
import cors from 'cors'
import { restrictUser } from "./routes/admin/restrict-user";
import validateUserNotDeactivated from "./middlewares/user-deactivation";
import { deleteCustomer } from "./routes/admin/delete-customer";
import { listDeletedCustomers } from "./routes/admin/list-deleted";
import { restoreCustomer } from "./routes/admin/restore-customer";

export default (rootDirectory: string): Router | Router[] => {
const { configModule: { projectConfig } } = getConfigFile<ConfigModule>(rootDirectory, "medusa-config")
Expand All @@ -18,11 +18,8 @@ export default (rootDirectory: string): Router | Router[] => {
const router = Router();
router.use(cors(storefrontCorsConfig))

// middlewares
router.use('/store/auth', express.json(), validateUserNotDeactivated);

// endpoints
const endpointHandlers = [restrictUser, deleteCustomer]
const endpointHandlers = [deleteCustomer, listDeletedCustomers, restoreCustomer]
endpointHandlers.forEach(endpointHandle => endpointHandle(router))

return router
Expand Down
15 changes: 0 additions & 15 deletions src/api/middlewares/user-deactivation.ts

This file was deleted.

13 changes: 6 additions & 7 deletions src/api/routes/admin/delete-customer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { authenticate } from "@medusajs/medusa"
import { CustomerService, authenticate } from "@medusajs/medusa"
import express, { Router } from "express";
import DeletedCustomerService from "../../../services/deleted-customer";

export const deleteCustomer = (router: Router) => {
router.use('/admin/delete-customer', express.json());
router.use('/admin/customers/delete', express.json(), authenticate());

router.post('/admin/delete-customer', authenticate(), async (req, res) => {
const deletedCustomerService = req.scope.resolve('deletedCustomerService') as DeletedCustomerService;
router.post('/admin/customers/delete', async (req, res) => {
try {
await deletedCustomerService.deleteCustomer(req.body.email);
const customerService = req.scope.resolve('customerService') as CustomerService;
const customer = await customerService.retrieveRegisteredByEmail(req.body.email)
await customerService.delete(customer.id)
return res.json({ status: 200, message: 'User has been deleted!' })
} catch (error) {
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error })
}

})
}
17 changes: 17 additions & 0 deletions src/api/routes/admin/list-deleted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { authenticate } from "@medusajs/medusa"
import { Router } from "express";
import CustomerService from "../../../services/customer";

export const listDeletedCustomers = (router: Router) => {
router.use('/admin/customers/list-deleted', authenticate());

router.get('/admin/customers/list-deleted', async (req, res) => {
const customerService = req.scope.resolve('customerService') as CustomerService
try {
const result = await customerService.retrieveDeletedCustomer()
return res.json(result)
} catch (error) {
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error })
}
})
}
21 changes: 21 additions & 0 deletions src/api/routes/admin/restore-customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { authenticate } from "@medusajs/medusa"
import express, { Router } from "express";
import CustomerService from "../../../services/customer";

export const restoreCustomer = (router: Router) => {
router.use('/admin/customers/restore', express.json(), authenticate());

router.post('/admin/customers/restore', async (req, res) => {

if (!req.body.email) return res.status(422).json({ status: 422, message: 'Insufficient payload!' })

try {
const customerService = req.scope.resolve('customerService') as CustomerService
await customerService.restoreCustomer(req.body.email);

return res.json({ status: 200, message: 'User has been restored!' })
} catch (error) {
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error })
}
})
}
18 changes: 0 additions & 18 deletions src/api/routes/admin/restrict-user.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/migrations/1696512365971-DeletedCustomer.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/migrations/1696513431383-UpdateFieldDeleteCustomer.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/migrations/1696513616474-UpdateFieldDeleteCustomer.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/migrations/1696520782799-UpdateFieldDeleteCustomer.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/migrations/1696521959509-AddUniqueConstraints.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/models/deleted-customer.ts

This file was deleted.

39 changes: 39 additions & 0 deletions src/services/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CustomerService as BaseCustomerService, Customer } from '@medusajs/medusa'

export default class CustomerService extends BaseCustomerService {
constructor(container) {
super(container)
}


async retrieveDeletedCustomer() {
const deletedCustomerQuery = `
SELECT id, email, first_name, last_name, billing_address_id, phone, has_account, metadata, created_at, updated_at, deleted_at FROM customer WHERE deleted_at IS NOT NULL
`

const listOfDeletedCustomers = await this.activeManager_.query(deletedCustomerQuery)

return listOfDeletedCustomers
}

async restoreCustomer(email: string) {
// check if email exists!

const customerByEmailQuery = `
SELECT * FROM customer WHERE email = $1
`

const customerResult = await this.activeManager_.query(customerByEmailQuery, [email]) as Customer[]

if (!customerResult.length) {
console.log(customerResult);
throw new Error('Customer not found with this email!')
}

const restoreCustomerQuery = `
UPDATE customer SET deleted_at = NULL WHERE email = $1
`

await this.activeManager_.query(restoreCustomerQuery, [email])
}
}
53 changes: 0 additions & 53 deletions src/services/deleted-customer.ts

This file was deleted.

0 comments on commit fe9898b

Please sign in to comment.