Skip to content
Open
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
1,252 changes: 916 additions & 336 deletions POS/src/components/settings/POSSettings.vue

Large diffs are not rendered by default.

94 changes: 41 additions & 53 deletions POS/src/composables/usePermissions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { call } from "@/utils/apiWrapper"
import { computed, ref } from "vue"
import { call } from "@/utils/apiWrapper";
import { computed, ref } from "vue";

/**
* Composable for checking user permissions
* Provides reactive permission checks for doctypes
*/

// Cache permissions to avoid repeated API calls
const permissionCache = ref({})
const permissionCache = ref({});

export function usePermissions() {
/**
Expand All @@ -20,31 +20,28 @@ export function usePermissions() {
async function checkPermission(doctype, permType = "create", docname = null) {
try {
// Check cache first
const cacheKey = `${doctype}:${permType}`
const cacheKey = `${doctype}:${permType}`;
if (permissionCache.value[cacheKey] !== undefined) {
return permissionCache.value[cacheKey]
return permissionCache.value[cacheKey];
}

// Call backend to check permission
const result = await call("frappe.client.has_permission", {
doctype: doctype,
docname: docname || "",
perm_type: permType,
})
});

// Extract has_permission from response object
const hasPermission = Boolean(result?.has_permission)
const hasPermission = Boolean(result?.has_permission);

// Cache the result
permissionCache.value[cacheKey] = hasPermission
return hasPermission
permissionCache.value[cacheKey] = hasPermission;
return hasPermission;
} catch (error) {
console.error(
`Error checking permission for ${doctype}:${permType}`,
error,
)
console.error(`Error checking permission for ${doctype}:${permType}`, error);
// Default to false on error (safer)
return false
return false;
}
}

Expand All @@ -54,16 +51,16 @@ export function usePermissions() {
* @returns {Promise<Object>} - Object with results keyed by "doctype:permType"
*/
async function checkMultiplePermissions(checks) {
const results = {}
const results = {};

await Promise.all(
checks.map(async ({ doctype, permType = "create" }) => {
const key = `${doctype}:${permType}`
results[key] = await checkPermission(doctype, permType)
}),
)
const key = `${doctype}:${permType}`;
results[key] = await checkPermission(doctype, permType);
})
);

return results
return results;
}

/**
Expand All @@ -73,33 +70,33 @@ export function usePermissions() {
* @returns {Object} - { hasPermission, loading, checkPermission }
*/
function usePermissionCheck(doctype, permType = "create") {
const hasPermission = ref(false)
const loading = ref(false)
const hasPermission = ref(false);
const loading = ref(false);

const check = async () => {
loading.value = true
loading.value = true;
try {
hasPermission.value = await checkPermission(doctype, permType)
hasPermission.value = await checkPermission(doctype, permType);
} finally {
loading.value = false
loading.value = false;
}
}
};

// Check immediately
check()
check();

return {
hasPermission: computed(() => hasPermission.value),
loading: computed(() => loading.value),
refresh: check,
}
};
}

/**
* Clear permission cache (useful after role changes)
*/
function clearCache() {
permissionCache.value = {}
permissionCache.value = {};
}

/**
Expand All @@ -116,9 +113,9 @@ export function usePermissions() {
{ doctype: "POS Coupon", permType: "write" },
{ doctype: "Sales Invoice", permType: "create" },
{ doctype: "Sales Invoice", permType: "submit" },
]
];

await checkMultiplePermissions(commonChecks)
await checkMultiplePermissions(commonChecks);
}

return {
Expand All @@ -127,43 +124,34 @@ export function usePermissions() {
usePermissionCheck,
clearCache,
preloadCommonPermissions,
}
};
}

/**
* Permission helper for common POS operations
*/
export function usePOSPermissions() {
const { checkPermission, preloadCommonPermissions } = usePermissions()
const { checkPermission, preloadCommonPermissions } = usePermissions();

// Customer permissions
const canCreateCustomer = async () =>
await checkPermission("Customer", "create")
const canEditCustomer = async () => await checkPermission("Customer", "write")
const canCreateCustomer = async () => await checkPermission("Customer", "create");
const canEditCustomer = async () => await checkPermission("Customer", "write");

// Promotion permissions
const canCreatePromotion = async () =>
await checkPermission("Promotional Scheme", "create")
const canEditPromotion = async () =>
await checkPermission("Promotional Scheme", "write")
const canDeletePromotion = async () =>
await checkPermission("Promotional Scheme", "delete")
const canCreatePromotion = async () => await checkPermission("Promotional Scheme", "create");
const canEditPromotion = async () => await checkPermission("Promotional Scheme", "write");
const canDeletePromotion = async () => await checkPermission("Promotional Scheme", "delete");

// Coupon permissions
const canCreateCoupon = async () =>
await checkPermission("POS Coupon", "create")
const canApplyCoupon = async () =>
await checkPermission("POS Coupon", "write")
const canCreateCoupon = async () => await checkPermission("POS Coupon", "create");
const canApplyCoupon = async () => await checkPermission("POS Coupon", "write");

// Invoice permissions
const canCreateInvoice = async () =>
await checkPermission("Sales Invoice", "create")
const canSubmitInvoice = async () =>
await checkPermission("Sales Invoice", "submit")
const canCreateInvoice = async () => await checkPermission("Sales Invoice", "create");
const canSubmitInvoice = async () => await checkPermission("Sales Invoice", "submit");

// Settings permissions
const canEditSettings = async () =>
await checkPermission("POS Settings", "write")
const canEditSettings = async () => await checkPermission("POS Next Settings", "write");

return {
canCreateCustomer,
Expand All @@ -177,5 +165,5 @@ export function usePOSPermissions() {
canSubmitInvoice,
canEditSettings,
preloadCommonPermissions,
}
};
}
Loading